8000 converted to Python 3x · jjjchens235/python-api-client@32f6184 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32f6184

Browse files
Justin WongJustin Wong
authored andcommitted
converted to Python 3x
1 parent ab6e2d4 commit 32f6184

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

MultipartPostHandler.py

100644100755
Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@
3838
then uploads it to the W3C validator.
3939
"""
4040

41-
import urllib
42-
import urllib2
43-
import mimetools, mimetypes
41+
import urllib.request, urllib.parse, urllib.error
42+
import urllib.request, urllib.error, urllib.parse
43+
#import mimetools
44+
import mimetypes
4445
import os, stat
46+
import string
47+
import random
4548

4649
class Callable:
4750
def __init__(self, anycallable):
@@ -51,40 +54,43 @@ def __init__(self, anycallable):
5154
# assigning a sequence.
5255
doseq = 1
5356

54-
class MultipartPostHandler(urllib2.BaseHandler):
55-
handler_order = urllib2.HTTPHandler.handler_order - 10 # needs to run first
57+
class MultipartPostHandler(urllib.request.BaseHandler):
58+
handler_order = urllib.request.HTTPHandler.handler_order - 10 # needs to run first
5659

5760
def http_request(self, request):
5861
data = request.get_data()
5962
if data is not None and type(data) != str:
6063
v_files = []
6164
v_vars = []
6265
try:
63-
for(key, value) in data.items():
66+
for(key, value) in list(data.items()):
6467
if type(value) == file:
6568
v_files.append((key, value))
6669
else:
6770
v_vars.append((key, value))
6871
except TypeError:
6972
systype, value, traceback = sys.exc_info()
70-
raise TypeError, "not a valid non-string sequence or mapping object", traceback
73+
raise TypeError("not a valid non-string sequence or mapping object").with_traceback(traceback)
7174

7275
if len(v_files) == 0:
73-
data = urllib.urlencode(v_vars, doseq)
76+
data = urllib.parse.urlencode(v_vars, doseq)
7477
else:
7578
boundary, data = self.multipart_encode(v_vars, v_files)
7679
contenttype = 'multipart/form-data; boundary=%s' % boundary
7780
if(request.has_header('Content-Type')
7881
and request.get_header('Content-Type').find('multipart/form-data') != 0):
79-
print "Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data')
82+
print("Replacing %s with %s" % (request.get_header('content-type'), 'multipart/form-data'))
8083
request.add_unredirected_header('Content-Type', contenttype)
8184

8285
request.add_data(data)
8386
return request
8487

88+
def id_generator(size=10, chars=string.ascii_uppercase + string.digits):
89+
return ''.join(random.choice(chars) for _ in range(size))
90+
8591
def multipart_encode(vars, files, boundary = None, buffer = None):
8692
if boundary is None:
87-
boundary = mimetools.choose_boundary()
93+
boundary = id_generator()
8894
if buffer is None:
8995
buffer = ''
9096
for(key, value) in vars:
@@ -103,23 +109,23 @@ def multipart_encode(vars, files, boundary = None, buffer = None):
103109
buffer += '\r\n' + fd.read() + '\r\n'
104110
buffer += '--%s--\r\n\r\n' % boundary
105111
return boundary, buffer
106-
8000 multipart_encode = Callable(multipart_encode)
107112

113+
multipart_encode = Callable(multipart_encode)
108114
https_request = http_request
109115

110116
def main():
111117
import tempfile, sys
112118

113119
validatorURL = "http://validator.w3.org/check"
114-
opener = urllib2.build_opener(MultipartPostHandler)
120+
opener = urllib.request.build_opener(MultipartPostHandler)
115121

116122
def validateFile(url):
117123
temp = tempfile.mkstemp(suffix=".html")
118124
os.write(temp[0], opener.open(url).read())
119125
params = { "ss" : "0", # show source
120126
"doctype" : "Inline",
121127
"uploaded_file" : open(temp[1], "rb") }
122-
print opener.open(validatorURL, params).read()
128+
print(opener.open(validatorURL, params).read())
123129
os.remove(temp[1])
124130

125131
if len(sys.argv[1:]) > 0:

0 commit comments

Comments
 (0)
0