Skip to content

Commit

Permalink
python 3 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
poelzi committed May 12, 2014
1 parent f7ca728 commit 4151787
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
File renamed without changes.
12 changes: 9 additions & 3 deletions rtorrent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import urllib
try:
import urllib.parse as urlparser
except ImportError:
import urllib as urlparser
import os.path
import time
import xmlrpclib
try:
import xmlrpc.client as xmlrpclib
except ImportError:
import xmlrpclib

from rtorrent.common import find_torrent, \
is_valid_port, convert_version_tuple_to_str
Expand Down Expand Up @@ -53,7 +59,7 @@ def __init__(self, uri, username=None, password=None,
self.username = username
self.password = password

self.schema = urllib.splittype(uri)[0]
self.schema = urlparser.splittype(uri)[0]

if sp:
self.sp = sp
Expand Down
5 changes: 4 additions & 1 deletion rtorrent/lib/xmlrpc/basic_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

from base64 import encodestring
import string
import xmlrpclib
try:
import xmlrpc.client as xmlrpclib
except:
import xmlrpclib


class BasicAuthTransport(xmlrpclib.Transport):
Expand Down
46 changes: 34 additions & 12 deletions rtorrent/lib/xmlrpc/scgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,25 @@
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
from __future__ import print_function

import httplib
try:
import http.client as httplib
except ImportError:
import httplib
import re
import socket
import urllib
import xmlrpclib
import sys
try:
import urllib.parse as urlparser
except ImportError:
import urllib as urlparser

try:
import xmlrpc.client as xmlrpclib
except:
import xmlrpclib

import errno


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

sock = None

try:
if host:
host, port = urllib.splitport(host)
host, port = urlparser.splitport(host)
addrinfo = socket.getaddrinfo(host, int(port), socket.AF_INET,
socket.SOCK_STREAM)
sock = socket.socket(*addrinfo[0][:3])
Expand All @@ -125,7 +138,10 @@ def single_request(self, host, handler, request_body, verbose=0):

self.verbose = verbose

sock.send(request_body)
if sys.version_info[0] > 2:
sock.send(bytes(request_body, "utf-8"))
else:
sock.send(request_body)
return self.parse_response(sock.makefile())
finally:
if sock:
Expand All @@ -142,11 +158,17 @@ def parse_response(self, response):
response_body += data

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

if self.verbose:
print 'body:', repr(response_body)
print('body:', repr(response_body))

try:
response_header, response_body = re.split(r'\n\s*?\n', response_body,
maxsplit=1)
except ValueError:
print("error in response: %s", response_body)
p.close()
u.close()

p.feed(response_body)
p.close()
Expand All @@ -157,10 +179,10 @@ def parse_response(self, response):
class SCGIServerProxy(xmlrpclib.ServerProxy):
def __init__(self, uri, transport=None, encoding=None, verbose=False,
allow_none=False, use_datetime=False):
type, uri = urllib.splittype(uri)
type, uri = urlparser.splittype(uri)
if type not in ('scgi'):
raise IOError('unsupported XML-RPC protocol')
self.__host, self.__handler = urllib.splithost(uri)
self.__host, self.__handler = urlparser.splithost(uri)
if not self.__handler:
self.__handler = '/'

Expand Down

0 comments on commit 4151787

Please sign in to comment.