Skip to content

Commit 4151787

Browse files
committed
python 3 fixes
1 parent f7ca728 commit 4151787

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed
File renamed without changes.

rtorrent/__init__.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
1818
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
1919
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20-
import urllib
20+
try:
21+
import urllib.parse as urlparser
22+
except ImportError:
23+
import urllib as urlparser
2124
import os.path
2225
import time
23-
import xmlrpclib
26+
try:
27+
import xmlrpc.client as xmlrpclib
28+
except ImportError:
29+
import xmlrpclib
2430

2531
from rtorrent.common import find_torrent, \
2632
is_valid_port, convert_version_tuple_to_str
@@ -53,7 +59,7 @@ def __init__(self, uri, username=None, password=None,
5359
self.username = username
5460
self.password = password
5561

56-
self.schema = urllib.splittype(uri)[0]
62+
self.schema = urlparser.splittype(uri)[0]
5763

5864
if sp:
5965
self.sp = sp

rtorrent/lib/xmlrpc/basic_auth.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
from base64 import encodestring
2424
import string
25-
import xmlrpclib
25+
try:
26+
import xmlrpc.client as xmlrpclib
27+
except:
28+
import xmlrpclib
2629

2730

2831
class BasicAuthTransport(xmlrpclib.Transport):

rtorrent/lib/xmlrpc/scgi.py

+34-12
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,25 @@
8080
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
8181
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
8282
# OF THIS SOFTWARE.
83+
from __future__ import print_function
8384

84-
import httplib
85+
try:
86+
import http.client as httplib
87+
except ImportError:
88+
import httplib
8589
import re
8690
import socket
87-
import urllib
88-
import xmlrpclib
91+
import sys
92+
try:
93+
import urllib.parse as urlparser
94+
except ImportError:
95+
import urllib as urlparser
96+
97+
try:
98+
import xmlrpc.client as xmlrpclib
99+
except:
100+
import xmlrpclib
101+
89102
import errno
90103

91104

@@ -96,7 +109,7 @@ def request(self, host, handler, request_body, verbose=0):
96109
for i in (0, 1):
97110
try:
98111
return self.single_request(host, handler, request_body, verbose)
99-
except socket.error, e:
112+
except socket.error as e:
100113
if i or e.errno not in (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE):
101114
raise
102115
except httplib.BadStatusLine: #close after we sent request
@@ -106,15 +119,15 @@ def request(self, host, handler, request_body, verbose=0):
106119
def single_request(self, host, handler, request_body, verbose=0):
107120
# Add SCGI headers to the request.
108121
headers = {'CONTENT_LENGTH': str(len(request_body)), 'SCGI': '1'}
109-
header = '\x00'.join(('%s\x00%s' % item for item in headers.iteritems())) + '\x00'
122+
header = '\x00'.join(('%s\x00%s' % item for item in headers.items())) + '\x00'
110123
header = '%d:%s' % (len(header), header)
111124
request_body = '%s,%s' % (header, request_body)
112125

113126
sock = None
114127

115128
try:
116129
if host:
117-
host, port = urllib.splitport(host)
130+
host, port = urlparser.splitport(host)
118131
addrinfo = socket.getaddrinfo(host, int(port), socket.AF_INET,
119132
socket.SOCK_STREAM)
120133
sock = socket.socket(*addrinfo[0][:3])
@@ -125,7 +138,10 @@ def single_request(self, host, handler, request_body, verbose=0):
125138

126139
self.verbose = verbose
127140

128-
sock.send(request_body)
141+
if sys.version_info[0] > 2:
142+
sock.send(bytes(request_body, "utf-8"))
143+
else:
144+
sock.send(request_body)
129145
return self.parse_response(sock.makefile())
130146
finally:
131147
if sock:
@@ -142,11 +158,17 @@ def parse_response(self, response):
142158
response_body += data
143159

144160
# Remove SCGI headers from the response.
145-
response_header, response_body = re.split(r'\n\s*?\n', response_body,
146-
maxsplit=1)
147161

148162
if self.verbose:
149-
print 'body:', repr(response_body)
163+
print('body:', repr(response_body))
164+
165+
try:
166+
response_header, response_body = re.split(r'\n\s*?\n', response_body,
167+
maxsplit=1)
168+
except ValueError:
169+
print("error in response: %s", response_body)
170+
p.close()
171+
u.close()
150172

151173
p.feed(response_body)
152174
p.close()
@@ -157,10 +179,10 @@ def parse_response(self, response):
157179
class SCGIServerProxy(xmlrpclib.ServerProxy):
158180
def __init__(self, uri, transport=None, encoding=None, verbose=False,
159181
allow_none=False, use_datetime=False):
160-
type, uri = urllib.splittype(uri)
182+
type, uri = urlparser.splittype(uri)
161183
if type not in ('scgi'):
162184
raise IOError('unsupported XML-RPC protocol')
163-
self.__host, self.__handler = urllib.splithost(uri)
185+
self.__host, self.__handler = urlparser.splithost(uri)
164186
if not self.__handler:
165187
self.__handler = '/'
166188

0 commit comments

Comments
 (0)