Port to python3, fix ssl support
This commit is contained in:
87
web.py
87
web.py
@@ -5,67 +5,68 @@ Author: Sean B. Palmer, inamidst.com
|
||||
About: http://inamidst.com/phenny/
|
||||
"""
|
||||
|
||||
import re, urllib
|
||||
from htmlentitydefs import name2codepoint
|
||||
import re, urllib.request, urllib.parse, urllib.error
|
||||
from html.entities import name2codepoint
|
||||
import json as jsonlib
|
||||
|
||||
class Grab(urllib.URLopener):
|
||||
def __init__(self, *args):
|
||||
self.version = 'Mozilla/5.0 (Phenny)'
|
||||
urllib.URLopener.__init__(self, *args)
|
||||
def http_error_default(self, url, fp, errcode, errmsg, headers):
|
||||
return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
|
||||
urllib._urlopener = Grab()
|
||||
class Grab(urllib.request.URLopener):
|
||||
def __init__(self, *args):
|
||||
self.version = 'Mozilla/5.0 (Phenny)'
|
||||
urllib.request.URLopener.__init__(self, *args)
|
||||
def http_error_default(self, url, fp, errcode, errmsg, headers):
|
||||
return urllib.addinfourl(fp, [headers, errcode], "http:" + url)
|
||||
urllib.request._urlopener = Grab()
|
||||
|
||||
def get(uri):
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
u = urllib.urlopen(uri)
|
||||
bytes = u.read()
|
||||
u.close()
|
||||
return bytes
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
u = urllib.request.urlopen(uri)
|
||||
bytes = u.read().decode('utf-8')
|
||||
u.close()
|
||||
return bytes
|
||||
|
||||
def head(uri):
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
u = urllib.urlopen(uri)
|
||||
info = u.info()
|
||||
u.close()
|
||||
return info
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
u = urllib.request.urlopen(uri)
|
||||
info = u.info()
|
||||
u.close()
|
||||
return info
|
||||
|
||||
def post(uri, query):
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
data = urllib.urlencode(query)
|
||||
u = urllib.urlopen(uri, data)
|
||||
bytes = u.read()
|
||||
u.close()
|
||||
return bytes
|
||||
if not uri.startswith('http'):
|
||||
return
|
||||
data = urllib.parse.urlencode(query)
|
||||
u = urllib.request.urlopen(uri, data)
|
||||
bytes = u.read().decode('utf-8')
|
||||
u.close()
|
||||
return bytes
|
||||
|
||||
r_entity = re.compile(r'&([^;\s]+);')
|
||||
|
||||
def entity(match):
|
||||
value = match.group(1).lower()
|
||||
if value.startswith('#x'):
|
||||
return unichr(int(value[2:], 16))
|
||||
elif value.startswith('#'):
|
||||
return unichr(int(value[1:]))
|
||||
elif name2codepoint.has_key(value):
|
||||
return unichr(name2codepoint[value])
|
||||
return '[' + value + ']'
|
||||
value = match.group(1).lower()
|
||||
if value.startswith('#x'):
|
||||
return chr(int(value[2:], 16))
|
||||
elif value.startswith('#'):
|
||||
return chr(int(value[1:]))
|
||||
elif value in name2codepoint:
|
||||
return chr(name2codepoint[value])
|
||||
return '[' + value + ']'
|
||||
|
||||
def quote(text):
|
||||
return urllib.parse.quote(text)
|
||||
|
||||
def decode(html):
|
||||
return r_entity.sub(entity, html)
|
||||
return r_entity.sub(entity, html)
|
||||
|
||||
r_string = re.compile(r'("(\\.|[^"\\])*")')
|
||||
r_json = re.compile(r'^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]+$')
|
||||
env = {'__builtins__': None, 'null': None, 'true': True, 'false': False}
|
||||
|
||||
def json(text):
|
||||
"""Evaluate JSON text safely (we hope)."""
|
||||
if r_json.match(r_string.sub('', text)):
|
||||
text = r_string.sub(lambda m: 'u' + m.group(1), text)
|
||||
return eval(text.strip(' \t\r\n'), env, {})
|
||||
raise ValueError('Input must be serialised JSON.')
|
||||
"""Evaluate JSON text safely (we hope)."""
|
||||
return jsonlib.loads(text)
|
||||
|
||||
if __name__=="__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user