Last active
December 12, 2015 08:49
-
-
Save gdvalle/4746726 to your computer and use it in GitHub Desktop.
hastebinit-like script for paste.pm. Python 2 and 3 compatible.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""""Client for paste.pm""" | |
import sys | |
PY3 = sys.version > '3' | |
if PY3: | |
from urllib.parse import urlencode | |
from urllib.request import Request, urlopen | |
else: | |
from urllib import urlencode | |
from urllib2 import Request, urlopen | |
from os import environ | |
def paste(*args): | |
if 'PASTE_SERVER' in environ: | |
SERVER_URL = environ['PASTE_SERVER'] | |
else: | |
SERVER_URL = 'http://paste.pm' | |
URL = SERVER_URL + '/post' | |
if args: | |
content = [open(x).read() for x in args] | |
else: | |
content = [sys.stdin.read()] | |
urls = [] | |
for data in content: | |
values = {'content': data} | |
postdata = urlencode(values) | |
if PY3: | |
postdata = postdata.encode('utf-8') | |
req = Request(URL, postdata) | |
response = urlopen(req) | |
path = response.read() | |
if PY3: | |
path = path.decode('utf-8') | |
url = SERVER_URL + path | |
urls.append(url) | |
return urls | |
def main(): | |
for url in paste(*sys.argv[1:]): | |
print(url) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment