/usr/share/pyshared/pyquery/openers.py is in python-pyquery 1.2.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | # -*- coding: utf-8 -*-
import sys
PY3k = sys.version_info >= (3,)
if PY3k:
from urllib.request import urlopen
from urllib.parse import urlencode
from urllib.parse import urljoin
basestring = (str, bytes)
else:
from urllib2 import urlopen # NOQA
from urllib import urlencode # NOQA
from urlparse import urljoin # NOQA
try:
import requests
HAS_REQUEST = True
except ImportError:
HAS_REQUEST = False
allowed_args = (
'auth', 'data', 'headers', 'verify', 'cert', 'config', 'hooks', 'proxies')
def _query(url, method, kwargs):
data = None
if 'data' in kwargs:
data = kwargs.pop('data')
if type(data) in (dict, list, tuple):
data = urlencode(data)
if isinstance(method, basestring) and \
method.lower() == 'get' and data:
if '?' not in url:
url += '?'
elif url[-1] not in ('?', '&'):
url += '&'
url += data
data = None
if data and PY3k:
data = data.encode('utf-8')
return url, data
def _requests(url, kwargs):
encoding = kwargs.get('encoding')
method = kwargs.get('method', 'get').lower()
meth = getattr(requests, str(method))
if method == 'get':
url, data = _query(url, method, kwargs)
kw = {}
for k in allowed_args:
if k in kwargs:
kw[k] = kwargs[k]
resp = meth(url=url, **kw)
if encoding:
resp.encoding = encoding
html = resp.content
return html
def _urllib(url, kwargs):
method = kwargs.get('method')
url, data = _query(url, method, kwargs)
return urlopen(url, data)
def url_opener(url, kwargs):
if HAS_REQUEST:
return _requests(url, kwargs)
return _urllib(url, kwargs)
|