chiark / gitweb /
add ubuntu-paste to send data to paste.ubuntu.com
authorColin Watson <cjwatson@chiark.greenend.org.uk>
Mon, 24 Nov 2008 19:44:15 +0000 (19:44 +0000)
committerColin Watson <cjwatson@chiark.greenend.org.uk>
Mon, 24 Nov 2008 19:44:15 +0000 (19:44 +0000)
ubuntu-paste [new file with mode: 0755]

diff --git a/ubuntu-paste b/ubuntu-paste
new file mode 100755 (executable)
index 0000000..b4317e9
--- /dev/null
@@ -0,0 +1,35 @@
+#!/usr/bin/python
+
+import httplib
+import os
+import sys
+import urllib
+from urlparse import urljoin
+
+paste_host = 'paste.ubuntu.com'
+paste_path = ''
+
+def http_post_form_with_auth(host, path, form):
+    form_data = urllib.urlencode(form).strip()
+    connection = httplib.HTTPConnection(host)
+    connection.request('POST', path, form_data, {
+        'Host': host,
+        'Content-type': 'application/x-www-form-urlencoded',
+        'Content-length': str(len(form_data)),
+        })
+    return connection.getresponse()
+
+if __name__ == '__main__':
+    poster = os.environ.get("USER")
+    syntax = 'text'
+    if len(sys.argv) > 1:
+        title = sys.argv[1]
+    else:
+        title = "The loser %s didn't even add a title" % poster
+    form = (('poster', poster), ('title', title), ('syntax', syntax), ('content', sys.stdin.read()))
+    response = http_post_form_with_auth(paste_host, paste_path, form)
+    location = response.getheader('Location')
+    if location:
+        print urljoin('http://' + paste_host, location)
+    else:
+        print 'Unexpected response:\n%s' % response.getheaders()