Change to Python3 coding style

This commit is contained in:
shortcutme 2019-03-15 21:06:59 +01:00
parent fc0fe0557b
commit b0b9a4d33c
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE
137 changed files with 910 additions and 913 deletions

View file

@ -6,11 +6,11 @@ import collections
import gevent
from cStringIO import StringIO
import io
from Debug import Debug
from Config import config
from util import helper
from PeerHashfield import PeerHashfield
from .PeerHashfield import PeerHashfield
from Plugin import PluginManager
if config.use_tempfiles:
@ -95,7 +95,7 @@ class Peer(object):
self.connection = connection_server.getConnection(self.ip, self.port, site=self.site, is_tracker_connection=self.is_tracker_connection)
self.reputation += 1
self.connection.sites += 1
except Exception, err:
except Exception as err:
self.onConnectionError("Getting connection error")
self.log("Getting connection error: %s (connection_error: %s, hash_failed: %s)" %
(Debug.formatException(err), self.connection_error, self.hash_failed))
@ -164,7 +164,7 @@ class Peer(object):
return res
else:
raise Exception("Invalid response: %s" % res)
except Exception, err:
except Exception as err:
if type(err).__name__ == "Notify": # Greenlet killed by worker
self.log("Peer worker got killed: %s, aborting cmd: %s" % (err.message, cmd))
break
@ -195,7 +195,7 @@ class Peer(object):
if config.use_tempfiles:
buff = tempfile.SpooledTemporaryFile(max_size=16 * 1024, mode='w+b')
else:
buff = StringIO()
buff = io.BytesIO()
s = time.time()
while True: # Read in smaller parts
@ -240,7 +240,7 @@ class Peer(object):
with gevent.Timeout(10.0, False): # 10 sec timeout, don't raise exception
res = self.request("ping")
if res and "body" in res and res["body"] == "Pong!":
if res and "body" in res and res["body"] == b"Pong!":
response_time = time.time() - s
break # All fine, exit from for loop
# Timeout reached or bad response
@ -267,12 +267,9 @@ class Peer(object):
request["peers_onion"] = packed_peers["onion"]
if packed_peers["ipv6"]:
request["peers_ipv6"] = packed_peers["ipv6"]
res = self.request("pex", request)
if not res or "error" in res:
return False
added = 0
# Remove unsupported peer types
@ -331,13 +328,13 @@ class Peer(object):
key = "peers"
else:
key = "peers_%s" % ip_type
for hash, peers in res.get(key, {}).items()[0:30]:
for hash, peers in list(res.get(key, {}).items())[0:30]:
if ip_type == "onion":
unpacker_func = helper.unpackOnionAddress
else:
unpacker_func = helper.unpackAddress
back[hash] += map(unpacker_func, peers)
back[hash] += list(map(unpacker_func, peers))
for hash in res.get("my", []):
back[hash].append((self.connection.ip, self.connection.port))

View file

@ -68,8 +68,8 @@ if __name__ == "__main__":
s = time.time()
for i in range(10000):
field.appendHashId(i)
print time.time()-s
print(time.time()-s)
s = time.time()
for i in range(10000):
field.hasHash("AABB")
print time.time()-s
print(time.time()-s)

View file

@ -1,6 +1,6 @@
import logging
import urllib
import urllib2
import urllib.request
import urllib.parse
import re
import time
@ -16,10 +16,10 @@ class PeerPortchecker(object):
def requestUrl(self, url, post_data=None):
if type(post_data) is dict:
post_data = urllib.urlencode(post_data)
req = urllib2.Request(url, post_data)
post_data = urllib.parse.urlencode(post_data).encode("utf8")
req = urllib.request.Request(url, post_data)
req.add_header('Referer', url)
return urllib2.urlopen(req, timeout=20.0)
return urllib.request.urlopen(req, timeout=20.0)
def portOpen(self, port):
self.log.info("Trying to open port using UpnpPunch...")
@ -67,7 +67,7 @@ class PeerPortchecker(object):
return res
def checkCanyouseeme(self, port):
data = urllib2.urlopen("http://www.canyouseeme.org/", "port=%s" % port, timeout=20.0).read()
data = urllib.request.urlopen("http://www.canyouseeme.org/", b"port=%s" % str(port).encode("ascii"), timeout=20.0).read().decode("utf8")
message = re.match('.*<p style="padding-left:15px">(.*?)</p>', data, re.DOTALL).group(1)
message = re.sub("<.*?>", "", message.replace("<br>", " ").replace("&nbsp;", " ")) # Strip http tags
@ -85,7 +85,7 @@ class PeerPortchecker(object):
raise Exception("Invalid response: %s" % message)
def checkPortchecker(self, port):
data = urllib2.urlopen("https://portchecker.co/check", "port=%s" % port, timeout=20.0).read()
data = urllib.request.urlopen("https://portchecker.co/check", b"port=%s" % str(port).encode("ascii"), timeout=20.0).read().decode("utf8")
message = re.match('.*<div id="results-wrapper">(.*?)</div>', data, re.DOTALL).group(1)
message = re.sub("<.*?>", "", message.replace("<br>", " ").replace("&nbsp;", " ").strip()) # Strip http tags
@ -109,7 +109,6 @@ class PeerPortchecker(object):
ip = re.match('.*Your IP is.*?name="host".*?value="(.*?)"', data, re.DOTALL).group(1)
token = re.match('.*name="token".*?value="(.*?)"', data, re.DOTALL).group(1)
print ip
post_data = {"host": ip, "port": port, "allow": "on", "token": token, "submit": "Scanning.."}
data = self.requestUrl(url, post_data).read()
@ -168,4 +167,4 @@ if __name__ == "__main__":
peer_portchecker = PeerPortchecker()
for func_name in ["checkIpv6scanner", "checkMyaddr", "checkPortchecker", "checkCanyouseeme"]:
s = time.time()
print(func_name, getattr(peer_portchecker, func_name)(3894), "%.3fs" % (time.time() - s))
print((func_name, getattr(peer_portchecker, func_name)(3894), "%.3fs" % (time.time() - s)))

View file

@ -1,2 +1,2 @@
from Peer import Peer
from PeerHashfield import PeerHashfield
from .Peer import Peer
from .PeerHashfield import PeerHashfield