phenny/web.py

69 lines
1.9 KiB
Python
Raw Permalink Normal View History

2012-12-16 17:09:43 -05:00
#!/usr/bin/env python3
"""
web.py - Web Facilities
Author: Sean B. Palmer, inamidst.com
About: http://inamidst.com/phenny/
"""
2013-06-09 01:27:24 -04:00
import re
import urllib.parse
import requests
2011-09-22 14:17:27 -04:00
import json as jsonlib
2013-06-09 01:27:24 -04:00
from requests.exceptions import ConnectionError, HTTPError, InvalidURL
from html.entities import name2codepoint
from urllib.parse import quote, unquote
user_agent = "Mozilla/5.0 (Phenny)"
default_headers = {'User-Agent': user_agent}
2013-06-09 01:27:24 -04:00
def get(uri, headers={}, verify=True, **kwargs):
2011-09-22 14:17:27 -04:00
if not uri.startswith('http'):
return
2013-06-09 01:27:24 -04:00
headers.update(default_headers)
r = requests.get(uri, headers=headers, verify=verify, **kwargs)
r.raise_for_status()
return r.text
2013-06-09 01:27:24 -04:00
def head(uri, headers={}, verify=True, **kwargs):
2011-09-22 14:17:27 -04:00
if not uri.startswith('http'):
return
2013-06-09 01:27:24 -04:00
headers.update(default_headers)
r = requests.head(uri, headers=headers, verify=verify, **kwargs)
r.raise_for_status()
return r.headers
2013-06-09 01:27:24 -04:00
def post(uri, data, headers={}, verify=True, **kwargs):
2011-09-22 14:17:27 -04:00
if not uri.startswith('http'):
return
2013-06-09 01:27:24 -04:00
headers.update(default_headers)
r = requests.post(uri, data=data, headers=headers, verify=verify, **kwargs)
r.raise_for_status()
return r.text
r_entity = re.compile(r'&([^;\s]+);')
def entity(match):
2011-09-22 14:17:27 -04:00
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 decode(html):
2011-09-22 14:17:27 -04:00
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):
2011-09-22 14:17:27 -04:00
"""Evaluate JSON text safely (we hope)."""
return jsonlib.loads(text)
if __name__=="__main__":
2011-09-22 14:17:27 -04:00
main()