1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/usr/bin/env python
# Copyright 2004-2008 Canonical Ltd. All rights reserved.
import base64
import httplib
import os
import pwd
import sys
import urllib
import webbrowser
from optparse import OptionParser
from urlparse import urljoin
# Should we be able to override any of these?
AUTH_FILE = '~/.canonical_paste_auth'
PASTE_HOST = 'pastebin.canonical.com'
PASTE_PATH = ''
def parse_arguments():
parser = OptionParser(usage='%prog [options] [title] < stdin')
parser.add_option('-b', '--browser',
default=False, action='store_true',
help='Open web browser to the pastebin.')
parser.add_option('-s', '--syntax',
default='text', type='string',
help='The syntax of the pastebin.')
parser.add_option('-f', '--file',
type='string',
help='File to pastebin instead of stdin.')
options, arguments = parser.parse_args()
if len(arguments) == 0:
parser.title = None
elif len(arguments) == 1:
parser.title = arguments[0]
else:
parser.error('Too many arguments')
# Does not return
parser.options = options
parser.arguments = arguments
return parser
def read_auth_file(path):
path = os.path.expanduser(path)
f = open(path)
try:
username = f.readline().strip()
password = f.readline().strip()
finally:
f.close()
return username, password
def make_basic_auth_header(username, password):
auth = '%s:%s' % (username, password)
return 'Basic %s' % base64.encodestring(auth).strip()
def https_post_form_with_auth(host, path, form, auth):
form_data = urllib.urlencode(form).strip()
connection = httplib.HTTPSConnection(host)
connection.request('POST', path, form_data, {
'Host': host,
'Authorization': auth,
'Content-type': 'application/x-www-form-urlencoded',
'Content-length': str(len(form_data)),
})
return connection.getresponse()
def main():
parser = parse_arguments()
try:
poster = os.environ['USER']
except KeyError:
poster = pwd.getpwuid(os.getuid()).pw_name
if parser.title is None:
title = "The loser %s didn't even add a title" % poster
else:
title = parser.title
if parser.options.file:
f = open(parser.options.file)
try:
content = f.read()
finally:
f.close()
else:
content = sys.stdin.read()
form = (
('poster', poster),
('title', title),
('syntax', parser.options.syntax),
('content', content),
)
# Figure out the authentication.
username, password = read_auth_file(AUTH_FILE)
auth = make_basic_auth_header(username, password)
response = https_post_form_with_auth(PASTE_HOST, PASTE_PATH, form, auth)
location = response.getheader('Location')
if location:
url = urljoin('https://' + PASTE_HOST, location)
print url
if parser.options.browser:
webbrowser.open(url)
else:
print 'Unexpected response:\n%s' % response.getheaders()
if __name__ == '__main__':
main()
|