From b238980e7533a97a6e14c1fdbc7f4da91e924001 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Fri, 22 Dec 2017 13:20:47 +0000 Subject: [PATCH] ubuntu-paste: convert to python3/argparse/requests --- ubuntu-paste | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/ubuntu-paste b/ubuntu-paste index b4317e9..a4ec503 100755 --- a/ubuntu-paste +++ b/ubuntu-paste @@ -1,35 +1,29 @@ -#!/usr/bin/python +#! /usr/bin/python3 -import httplib +from argparse import ArgumentParser import os import sys -import urllib -from urlparse import urljoin +from urllib.parse import urljoin + +import requests + 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') + + parser = ArgumentParser() + parser.add_argument("-f", "--format", default="text", help="format of paste") + parser.add_argument("title", default="The loser %s didn't even add a title" % poster, nargs="?") + args = parser.parse_args() + + form = (('poster', poster), ('title', args.title), ('syntax', args.format), ('content', sys.stdin.read())) + response = requests.post('https://%s/%s' % (paste_host, paste_path.lstrip('/')), data=form, allow_redirects=False) + location = response.headers['Location'] if location: - print urljoin('http://' + paste_host, location) + print(urljoin('https://' + paste_host, location)) else: - print 'Unexpected response:\n%s' % response.getheaders() + print('Unexpected response:\n%s' % response.headers) -- 2.30.2