Index: Lib/distutils/command/upload.py =================================================================== --- Lib/distutils/command/upload.py (revision 67190) +++ Lib/distutils/command/upload.py (working copy) @@ -7,11 +7,11 @@ from distutils.spawn import spawn from distutils import log from hashlib import md5 -import os +import os, io import socket import platform import configparser -import http.client +import http.client as httpclient import base64 import urllib.parse @@ -77,7 +77,7 @@ 'version': meta.get_version(), # file content - 'content': (os.path.basename(filename),content), + 'content': (os.path.basename(filename), content), 'filetype': command, 'pyversion': pyversion, 'md5_digest': md5(content).hexdigest(), @@ -113,33 +113,35 @@ open(filename+".asc").read()) # set up the authentication - auth = "Basic " + base64.encodestring(self.username + ":" + self.password).strip() + user_pass = (self.username + ":" + self.password).encode('ascii') + # The exact encoding of the authentication string is debated. + # Anyway PyPI only accepts ascii for both username or password. + auth = "Basic " + base64.encodestring(user_pass).strip().decode('ascii') # Build up the MIME payload for the POST data - boundary = '--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' - sep_boundary = '\n--' + boundary - end_boundary = sep_boundary + '--' - body = io.StringIO() + boundary = b'--------------GHSKFJDLGDS7543FJKLFHRE75642756743254' + sep_boundary = b'\n--' + boundary + end_boundary = sep_boundary + b'--' + body = io.BytesIO() for key, value in data.items(): # handle multiple entries for the same name if type(value) != type([]): value = [value] for value in value: + title = '\nContent-Disposition: form-data; name="%s"' % key if type(value) is tuple: - fn = ';filename="%s"' % value[0] + title += '; filename="%s"' % value[0] value = value[1] else: - fn = "" - value = str(value) + value = str(value).encode('utf-8') body.write(sep_boundary) - body.write('\nContent-Disposition: form-data; name="%s"'%key) - body.write(fn) - body.write("\n\n") + body.write(title.encode('utf-8')) + body.write(b"\n\n") body.write(value) - if value and value[-1] == '\r': - body.write('\n') # write an extra newline (lurve Macs) + if value and value[-1:] == b'\r': + body.write(b'\n') # write an extra newline (lurve Macs) body.write(end_boundary) - body.write("\n") + body.write(b"\n") body = body.getvalue() self.announce("Submitting %s to %s" % (filename, self.repository), log.INFO) @@ -152,23 +154,19 @@ urllib.parse.urlparse(self.repository) assert not params and not query and not fragments if schema == 'http': - http = http.client.HTTPConnection(netloc) + http = httpclient.HTTPConnection(netloc) elif schema == 'https': - http = http.client.HTTPSConnection(netloc) + http = httpclient.HTTPSConnection(netloc) else: raise AssertionError("unsupported schema "+schema) - data = '' - loglevel = log.INFO try: http.connect() - http.putrequest("POST", url) - http.putheader('Content-type', - 'multipart/form-data; boundary=%s'%boundary) - http.putheader('Content-length', str(len(body))) - http.putheader('Authorization', auth) - http.endheaders() - http.send(body) + headers = { + 'Content-type': 'multipart/form-data; boundary=%s' % boundary, + 'Authorization': auth, + } + http.request("POST", url, body, headers) except socket.error as e: self.announce(str(e), log.ERROR) return