Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to pyhton3 #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 87 additions & 75 deletions core/db.py

Large diffs are not rendered by default.

87 changes: 43 additions & 44 deletions core/dependence/urllib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,28 @@

import base64
import hashlib
import httplib
import mimetools
import http.client
import email
import os
import posixpath
import random
import re
import socket
import sys
import time
import urlparse
import urllib.parse
import bisect

try:
from cStringIO import StringIO
from io import StringIO
except ImportError:
from StringIO import StringIO
from io import StringIO

from urllib import (unwrap, unquote, splittype, splithost, quote,
addinfourl, splitport, splittag,
splitattr, ftpwrapper, splituser, splitpasswd, splitvalue)
import urllib.request, urllib.parse, urllib.error, urllib.response

# support for FileHandler, proxies via environment variables
from urllib import localhost, url2pathname, getproxies, proxy_bypass


from urllib.request import url2pathname

# used in User-Agent header sent
__version__ = sys.version[:3]
Expand Down Expand Up @@ -146,9 +145,9 @@ def __init__(self, reason):
def __str__(self):
return '<urlopen error %s>' % self.reason

class HTTPError(URLError, addinfourl):
class HTTPError(URLError, urllib.response.addinfourl):
"""Raised when HTTP error occurs, but also acts like non-error return"""
__super_init = addinfourl.__init__
__super_init = urllib.response.addinfourl.__init__

def __init__(self, url, code, msg, hdrs, fp):
self.code = code
Expand Down Expand Up @@ -182,7 +181,7 @@ def request_host(request):

"""
url = request.get_full_url()
host = urlparse.urlparse(url)[1]
host = urllib.parse.urlparse(url)[1]
if host == "":
host = request.get_header("Host", "")

Expand All @@ -204,7 +203,7 @@ def __init__(self, url, data=None, headers={},
self._tunnel_host = None
self.data = data
self.headers = {}
for key, value in headers.items():
for key, value in list(headers.items()):
self.add_header(key, value)
self.unredirected_hdrs = {}
if origin_req_host is None:
Expand All @@ -222,7 +221,7 @@ def __getattr__(self, attr):
if hasattr(Request, 'get_' + name):
getattr(self, 'get_' + name)()
return getattr(self, attr)
raise AttributeError, attr
raise AttributeError(attr)

def get_method(self):
if self.has_data():
Expand Down Expand Up @@ -251,7 +250,7 @@ def get_type(self):
if self.type is None:
self.type, self.__r_type = splittype(self.__original)
if self.type is None:
raise ValueError, "unknown url type: %s" % self.__original
raise ValueError("unknown url type: %s" % self.__original)
return self.type

def get_host(self):
Expand Down Expand Up @@ -302,7 +301,7 @@ def get_header(self, header_name, default=None):
def header_items(self):
hdrs = self.unredirected_hdrs.copy()
hdrs.update(self.headers)
return hdrs.items()
return list(hdrs.items())

class OpenerDirector:
def __init__(self):
Expand Down Expand Up @@ -381,7 +380,7 @@ def _call_chain(self, chain, kind, meth_name, *args):

def open(self, fullurl, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
# accept a URL or a Request object
if isinstance(fullurl, basestring):
if isinstance(fullurl, str):
req = Request(fullurl, data)
else:
req = fullurl
Expand Down Expand Up @@ -458,7 +457,7 @@ def build_opener(*handlers):
"""
import types
def isclass(obj):
return isinstance(obj, (types.ClassType, type))
return isinstance(obj, type)

opener = OpenerDirector()
default_classes = [ProxyHandler, UnknownHandler, HTTPHandler,
Expand Down Expand Up @@ -554,7 +553,7 @@ def redirect_request(self, req, fp, code, msg, headers, newurl):
# do the same.
# be conciliant with URIs containing a space
newurl = newurl.replace(' ', '%20')
newheaders = dict((k,v) for k,v in req.headers.items()
newheaders = dict((k,v) for k,v in list(req.headers.items())
if k.lower() not in ("content-length", "content-type")
)
return Request(newurl,
Expand All @@ -579,13 +578,13 @@ def http_error_302(self, req, fp, code, msg, headers):
return

# fix a possible malformed URL
urlparts = urlparse.urlparse(newurl)
urlparts = urllib.parse.urlparse(newurl)
if not urlparts.path:
urlparts = list(urlparts)
urlparts[2] = "/"
newurl = urlparse.urlunparse(urlparts)
newurl = urllib.parse.urlunparse(urlparts)

newurl = urlparse.urljoin(req.get_full_url(), newurl)
newurl = urllib.parse.urljoin(req.get_full_url(), newurl)

# For security reasons we do not allow redirects to protocols
# other than HTTP, HTTPS or FTP.
Expand Down Expand Up @@ -712,7 +711,7 @@ def __init__(self, proxies=None):
proxies = getproxies()
assert hasattr(proxies, 'has_key'), "proxies must be a mapping"
self.proxies = proxies
for type, url in proxies.items():
for type, url in list(proxies.items()):
setattr(self, '%s_open' % type,
lambda r, proxy=url, type=type, meth=self.proxy_open: \
meth(r, proxy, type))
Expand Down Expand Up @@ -753,7 +752,7 @@ def __init__(self):

def add_password(self, realm, uri, user, passwd):
# uri could be a single URI or a sequence
if isinstance(uri, basestring):
if isinstance(uri, str):
uri = [uri]
if not realm in self.passwd:
self.passwd[realm] = {}
Expand All @@ -766,7 +765,7 @@ def find_user_password(self, realm, authuri):
domains = self.passwd.get(realm, {})
for default_port in True, False:
reduced_authuri = self.reduce_uri(authuri, default_port)
for uris, authinfo in domains.iteritems():
for uris, authinfo in list(domains.items()):
for uri in uris:
if self.is_suburi(uri, reduced_authuri):
return authinfo
Expand All @@ -775,7 +774,7 @@ def find_user_password(self, realm, authuri):
def reduce_uri(self, uri, default_port=True):
"""Accept authority or URI and extract only the authority and path."""
# note HTTP URLs do not have a userinfo component
parts = urlparse.urlsplit(uri)
parts = urllib.parse.urlsplit(uri)
if parts[1]:
# URI
scheme = parts[0]
Expand Down Expand Up @@ -1073,7 +1072,7 @@ class HTTPDigestAuthHandler(BaseHandler, AbstractDigestAuthHandler):
handler_order = 490 # before Basic auth

def http_error_401(self, req, fp, code, msg, headers):
host = urlparse.urlparse(req.get_full_url())[1]
host = urllib.parse.urlparse(req.get_full_url())[1]
retry = self.http_error_auth_reqed('www-authenticate',
host, req, headers)
self.reset_retry_count()
Expand Down Expand Up @@ -1147,7 +1146,7 @@ def do_open(self, http_class, req):
h.set_debuglevel(self._debuglevel)

headers = dict(req.unredirected_hdrs)
headers.update(dict((k, v) for k, v in req.headers.items()
headers.update(dict((k, v) for k, v in list(req.headers.items())
if k not in headers))

# We want to make an HTTP/1.1 request, but the addinfourl
Expand All @@ -1158,7 +1157,7 @@ def do_open(self, http_class, req):
# request.
headers["Connection"] = "close"
headers = dict(
(name.title(), val) for name, val in headers.items())
(name.title(), val) for name, val in list(headers.items()))

if req._tunnel_host:
tunnel_headers = {}
Expand All @@ -1172,7 +1171,7 @@ def do_open(self, http_class, req):

try:
h.request(req.get_method(), req.get_selector(), req.data, headers)
except socket.error, err: # XXX what error?
except socket.error as err: # XXX what error?
h.close()
raise URLError(err)
else:
Expand Down Expand Up @@ -1204,23 +1203,23 @@ def do_open(self, http_class, req):
class HTTPHandler(AbstractHTTPHandler):

def http_open(self, req):
return self.do_open(httplib.HTTPConnection, req)
return self.do_open(http.client.HTTPConnection, req)

http_request = AbstractHTTPHandler.do_request_

if hasattr(httplib, 'HTTPS'):
if hasattr(http.client, 'HTTPS'):
class HTTPSHandler(AbstractHTTPHandler):

def https_open(self, req):
return self.do_open(httplib.HTTPSConnection, req)
return self.do_open(http.client.HTTPSConnection, req)

https_request = AbstractHTTPHandler.do_request_

class HTTPCookieProcessor(BaseHandler):
def __init__(self, cookiejar=None):
import cookielib
import http.cookiejar
if cookiejar is None:
cookiejar = cookielib.CookieJar()
cookiejar = http.cookiejar.CookieJar()
self.cookiejar = cookiejar

def http_request(self, request):
Expand Down Expand Up @@ -1345,7 +1344,7 @@ def open_local_file(self, req):
else:
origurl = 'file://' + filename
return addinfourl(open(localfile, 'rb'), headers, origurl)
except OSError, msg:
except OSError as msg:
# urllib2 users shouldn't expect OSErrors coming from urlopen()
raise URLError(msg)
raise URLError('file not on local host')
Expand Down Expand Up @@ -1375,11 +1374,11 @@ def ftp_open(self, req):

try:
host = socket.gethostbyname(host)
except socket.error, msg:
except socket.error as msg:
raise URLError(msg)
path, attrs = splitattr(req.get_selector())
dirs = path.split('/')
dirs = map(unquote, dirs)
dirs = list(map(unquote, dirs))
dirs, file = dirs[:-1], dirs[-1]
if dirs and not dirs[0]:
dirs = dirs[1:]
Expand All @@ -1401,8 +1400,8 @@ def ftp_open(self, req):
sf = StringIO(headers)
headers = mimetools.Message(sf)
return addinfourl(fp, headers, req.get_full_url())
except ftplib.all_errors, msg:
raise URLError, ('ftp error: %s' % msg), sys.exc_info()[2]
except ftplib.all_errors as msg:
raise URLError('ftp error: %s' % msg).with_traceback(sys.exc_info()[2])

def connect_ftp(self, user, passwd, host, port, dirs, timeout):
fw = ftpwrapper(user, passwd, host, port, dirs, timeout,
Expand Down Expand Up @@ -1440,7 +1439,7 @@ def check_cache(self):
# first check for old ones
t = time.time()
if self.soonest <= t:
for k, v in self.timeout.items():
for k, v in list(self.timeout.items()):
if v < t:
self.cache[k].close()
del self.cache[k]
Expand All @@ -1449,15 +1448,15 @@ def check_cache(self):

# then check the size
if len(self.cache) == self.max_conns:
for k, v in self.timeout.items():
for k, v in list(self.timeout.items()):
if v == self.soonest:
del self.cache[k]
del self.timeout[k]
break
self.soonest = min(self.timeout.values())

def clear_cache(self):
for conn in self.cache.values():
for conn in list(self.cache.values()):
conn.close()
self.cache.clear()
self.timeout.clear()
8 changes: 4 additions & 4 deletions core/ngrok.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, authtoken, port, nT, hash):
if authtoken:
self.token = authtoken
else:
print "Can't use Ngrok without a valid token"
print("Can't use Ngrok without a valid token")
system_type = os.name
system_name = platform.system()
system_architecture = platform.architecture()[0]
Expand All @@ -35,7 +35,7 @@ def __init__(self, authtoken, port, nT, hash):
if path.exists(str_ngrok):
pass
else:
import urllib2
import urllib.request, urllib.error, urllib.parse

if "posix" in system_type:
if "arwin" in system_name:
Expand All @@ -58,7 +58,7 @@ def __init__(self, authtoken, port, nT, hash):

filename = "ngrok.zip"

download = urllib2.urlopen(download_link)
download = urllib.request.urlopen(download_link)
saved_file=file(filename,"w")
saved_file.write(download.read())
saved_file.close()
Expand All @@ -79,4 +79,4 @@ def start_ngrok(port, hash, f=0):
if "nt" in system_type:
str_ngrok = './ngrok.exe'
result = subprocess.check_output([str_ngrok, "http", port])
print result
print(result)
2 changes: 1 addition & 1 deletion core/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from flask_socketio import SocketIO, emit, join_room, rooms, disconnect
import core.stats
import core.user
from user_objects import attacks_hook_message
from .user_objects import attacks_hook_message
from core.utils import utils
from core.db import Database
import sys
Expand Down
4 changes: 2 additions & 2 deletions core/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os
from flask import Flask, render_template, session, request, json, redirect, url_for, send_from_directory
from flask_cors import CORS
from trape import Trape
from .trape import Trape
from core.db import Database

# Main parts, to generate relationships among others
Expand Down Expand Up @@ -90,7 +90,7 @@ def home_get_preview():

@app.route("/get_title", methods=["POST"])
def home_get_title():
opener = urllib2.build_opener()
opener = urllib.request.build_opener()
html = opener.open(trape.url_to_clone).read()
html = html[html.find('<title>') + 7 : html.find('</title>')]
return json.dumps({'status' : 'OK', 'title' : html})
Expand Down
Loading