Skip to content

Instantly share code, notes, and snippets.

@sonya75
Created August 3, 2018 22:06
Show Gist options
  • Save sonya75/15c1b4a224e64f2fff228ac7e63ece63 to your computer and use it in GitHub Desktop.
Save sonya75/15c1b4a224e64f2fff228ac7e63ece63 to your computer and use it in GitHub Desktop.
# default data for the current version of chrome
USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
ACCEPT_HEADER="text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
ACCEPT_HEADER_AJAX="*/*"
ACCEPT_ENCODING_HEADER="gzip, deflate, br"
ACCEPT_LANGUAGE_HEADER="en-US,en;q=0.9,ms;q=0.8"
CONNECTION_HEADER="keep-alive"
DEFAULT_CONTENT_TYPE="application/x-www-form-urlencoded"
from requests.sessions import *
class ChromePreparedRequest(PreparedRequest):
def prepare_headers(self,headers):
self.headers=headers
class ChromeSession(Session):
def __init__(self):
self.headers = CaseInsensitiveDict()
self.auth = None
self.proxies = {}
self.hooks = default_hooks()
self.params = {}
self.stream = False
self.verify = True
self.cert = None
self.max_redirects = DEFAULT_REDIRECT_LIMIT
self.trust_env = True
self.cookies = cookiejar_from_dict({})
self.adapters = OrderedDict()
self.mount('https://', HTTPAdapter())
self.mount('http://', HTTPAdapter())
def request(self, method, url,
params=None, data=None, headers=None, cookies=None, files=None,
auth=None, timeout=None, allow_redirects=True, proxies=None,
hooks=None, stream=None, verify=None, cert=None, json=None,accept=None,contenttype=None,ajax=False):
""" Use the accept parameter insteas of "Accept" in the headers parameter when its a default custom
Accept header set by browser on certain types of requests and not set from javascript.
If its set from javascript use it in the headers dict instead.
Same goes for content-type."""
req = Request(
method=method.upper(),
url=url,
headers=headers,
files=files,
data=data or {},
json=json,
params=params or {},
auth=auth,
cookies=cookies,
hooks=hooks,
)
req.accept=accept
req.contenttype=contenttype
req.ajax=ajax
prep = self.prepare_request(req)
proxies = proxies or {}
settings = self.merge_environment_settings(
prep.url, proxies, stream, verify, cert
)
send_kwargs = {
'timeout': timeout,
'allow_redirects': allow_redirects,
}
send_kwargs.update(settings)
resp = self.send(prep, **send_kwargs)
return resp
def prepare_request(self, request):
cookies = request.cookies or {}
if not isinstance(cookies, cookielib.CookieJar):
cookies = cookiejar_from_dict(cookies)
merged_cookies = merge_cookies(
merge_cookies(RequestsCookieJar(), self.cookies), cookies)
auth = request.auth
if self.trust_env and not auth and not self.auth:
auth = get_netrc_auth(request.url)
headers=self.headers.copy()
headers.update(request.headers)
headerdata=CaseInsensitiveDict()
if "Host" in headers:
headerdata["Host"]=headers.pop("Host")
headerdata._store["connection"]=headers._store.pop("connection",None) or ("Connection",CONNECTION_HEADER)
request.method=request.method.upper()
if (request.method in ["POST","PUT"]) or request.data or (request.json is not None):
headerdata._store["content-length"]=headers._store.pop("content-length",None) or ("Content-Length","0")
if "Content-Type" not in headers:
headerdata["Content-Type"]=DEFAULT_CONTENT_TYPE
if request.contenttype is not None:
headerdata["Content-Type"]=request.contenttype
headers.pop("Content-Type",None)
if request.accept is None:
if request.ajax:
headerdata._store["accept"]=headers._store.pop("accept",None) or ("Accept",ACCEPT_HEADER_AJAX)
else:
headerdata._store["accept"]=headers._store.pop("accept",None) or ("Accept",ACCEPT_HEADER)
if "Origin" in headers:
headerdata["Origin"]=headers.pop("Origin")
headerdata._store["user-agent"]=headers._store.pop("user-agent",None) or ("User-Agent",USER_AGENT)
for p in headers:
headerdata[p]=headers[p]
if request.accept is not None:
headerdata["Accept"]=headerdata.pop("Accept",None) or "*/*"
if "Referer" in headerdata:
headerdata["Referer"]=headerdata.pop("Referer")
headerdata["Accept-Encoding"]=headerdata.pop("Accept-Encoding",None) or ACCEPT_ENCODING_HEADER
headerdata["Accept-Language"]=ACCEPT_LANGUAGE_HEADER
if "Cookie" in headerdata:
headerdata["Cookie"]=headerdata.pop("Cookie")
p = ChromePreparedRequest()
p.prepare(
method=request.method.upper(),
url=request.url,
files=request.files,
data=request.data,
json=request.json,
headers=headerdata,
params=merge_setting(request.params, self.params),
auth=merge_setting(auth, self.auth),
cookies=merged_cookies,
hooks=merge_hooks(request.hooks, self.hooks),
)
return p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment