本文整理汇总了Python中cookies.Cookies.from_request方法的典型用法代码示例。如果您正苦于以下问题:Python Cookies.from_request方法的具体用法?Python Cookies.from_request怎么用?Python Cookies.from_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cookies.Cookies
的用法示例。
在下文中一共展示了Cookies.from_request方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def __init__(self, args):
super(HttpScan, self).__init__(args)
self.session = requesocks.session()
adapters.DEFAULT_RETRIES = self.args.max_retries
self.tor = None
if self.args.tor:
self.out.log("Enabling TOR")
self.tor = Torify()
self.session.proxies = {'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'}
if self.args.check_tor:
# Check TOR
self.out.log("Checking IP via TOR")
rip, tip = self.tor.check_ip(verbose=True)
if tip is None:
self.out.log('TOR is not working properly!', logging.ERROR)
exit(-1)
if self.args.cookies is not None:
if path.exists(self.args.cookies) and path.isfile(self.args.cookies):
self.cookies = MozillaCookieJar(self.args.cookies)
self.cookies.load()
else:
# self.out.log('Could not find cookie file: %s' % self.args.load_cookies, logging.ERROR)
self.cookies = Cookies.from_request(self.args.cookies)
else:
self.cookies = None
self.ua = UserAgent() if self.args.user_agent is None else self.args.user_agent
示例2: _on_request
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def _on_request(self, adapter, request, **kwargs):
match = self._find_match(request)
# TODO(dcramer): find the correct class for this
if match is None:
error_msg = 'Connection refused: {0} {1}'.format(request.method,
request.url)
response = ConnectionError(error_msg)
response.request = request
self._calls.add(request, response)
raise response
if 'body' in match and isinstance(match['body'], Exception):
self._calls.add(request, match['body'])
raise match['body']
headers = {}
if match['content_type'] is not None:
headers['Content-Type'] = match['content_type']
if 'callback' in match: # use callback
status, r_headers, body = match['callback'](request)
if isinstance(body, six.text_type):
body = body.encode('utf-8')
body = BufferIO(body)
headers.update(r_headers)
elif 'body' in match:
if match['adding_headers']:
headers.update(match['adding_headers'])
status = match['status']
body = BufferIO(match['body'])
response = HTTPResponse(
status=status,
reason=six.moves.http_client.responses[status],
body=body,
headers=headers,
preload_content=False,
)
response = adapter.build_response(request, response)
if not match.get('stream'):
response.content # NOQA
try:
resp_cookies = Cookies.from_request(response.headers['set-cookie'])
response.cookies = cookiejar_from_dict(dict(
(v.name, v.value)
for _, v
in resp_cookies.items()
))
except (KeyError, TypeError):
pass
self._calls.add(request, response)
return response
示例3: _cookies_from_headers
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def _cookies_from_headers(headers):
try:
import http.cookies as cookies
resp_cookie = cookies.SimpleCookie()
resp_cookie.load(headers["set-cookie"])
cookies_dict = {name: v.value for name, v in resp_cookie.items()}
except ImportError:
from cookies import Cookies
resp_cookies = Cookies.from_request(headers["set-cookie"])
cookies_dict = {v.name: v.value for _, v in resp_cookies.items()}
return cookiejar_from_dict(cookies_dict)
示例4: obtain_session
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def obtain_session(self, env):
cookie_str = env.get('HTTP_COOKIE', None)
session = None
if cookie_str:
cookie = Cookies.from_request(
cookie_str,
ignore_bad_cookies=True,
).get('session', None)
if cookie and cookie.value:
if cookie.value in self.sessions:
# Session found
session = self.sessions[cookie.value]
if session.is_dead():
session = None
return session
示例5: __init__
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def __init__(self, cnxn, method, url, headers, body):
self.cnxn = cnxn
self.method = method
self.url = url
self.headers = headers
self.body = body
# A counter to help troubleshoot.
self._id = Request._next_id
Request._next_id = (Request._next_id + 1) % 1000000
self.form = self.parse_form()
c = headers.get('cookie', None)
if c:
self.cookies = Cookies.from_request(c)
else:
self.cookies = Cookies()
示例6: _on_request
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def _on_request(self, adapter, request, **kwargs):
match = self._find_match(request)
# TODO(dcramer): find the correct class for this
if match is None:
error_msg = "Connection refused: {0} {1}".format(request.method, request.url)
response = ConnectionError(error_msg)
self._calls.add(request, response)
raise response
if "body" in match and isinstance(match["body"], Exception):
self._calls.add(request, match["body"])
raise match["body"]
headers = {"Content-Type": match["content_type"]}
if "callback" in match: # use callback
status, r_headers, body = match["callback"](request)
if isinstance(body, six.text_type):
body = body.encode("utf-8")
body = BufferIO(body)
headers.update(r_headers)
elif "body" in match:
if match["adding_headers"]:
headers.update(match["adding_headers"])
status = match["status"]
body = BufferIO(match["body"])
response = HTTPResponse(status=status, body=body, headers=headers, preload_content=False)
response = adapter.build_response(request, response)
if not match.get("stream"):
response.content # NOQA
try:
resp_cookies = Cookies.from_request(response.headers["set-cookie"])
response.cookies = cookiejar_from_dict(dict((v.name, v.value) for _, v in resp_cookies.items()))
except (KeyError, TypeError):
pass
self._calls.add(request, response)
return response
示例7: _init_scan_options
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def _init_scan_options(self):
# Session
self.session = session()
self.session.timeout = self.args.timeout
self.session.verify = False
# TODO: debug and check
# self.session.mount("http://", HTTPAdapter(max_retries=self.args.max_retries))
# self.session.mount("https://", HTTPAdapter(max_retries=self.args.max_retries))
# http://stackoverflow.com/questions/15431044/can-i-set-max-retries-for-requests-request
# Max retries
adapters.DEFAULT_RETRIES = self.args.max_retries
# TOR
if self.args.tor:
self.output.write_log("TOR usage detected. Making some checks.")
self.session.proxies = {
'http': 'socks5://127.0.0.1:9050',
'https': 'socks5://127.0.0.1:9050'
}
url = 'http://ifconfig.me/ip'
real_ip, tor_ip = None, None
# Ger real IP address
try:
real_ip = get(url).text.strip()
except Exception as exception:
self.output.print_and_log("Couldn't get real IP address. Check yout internet connection.",
logging.ERROR)
self.output.write_log(str(exception), logging.ERROR)
exit(-1)
# Get TOR IP address
try:
tor_ip = self.session.get(url).text.strip()
except Exception as exception:
self.output.print_and_log("TOR socks proxy doesn't seem to be working.", logging.ERROR)
self.output.write_log(str(exception), logging.ERROR)
exit(-1)
# Show IP addresses
self.output.print_and_log('Real IP: %s TOR IP: %s' % (real_ip, tor_ip))
if real_ip == tor_ip:
self.output.print_and_log("TOR doesn't work! Stop to be secure.", logging.ERROR)
exit(-1)
# Proxy
if self.args.proxy is not None:
self.session.proxies = {"https": self.args.proxy,
"http": self.args.proxy}
# Auth
if self.args.auth is not None:
items = self.args.auth.split(':')
self.session.auth = (items[0], items[1])
# Cookies
self.cookies = {}
if self.args.cookies is not None:
self.cookies = Cookies.from_request(self.args.cookies)
# Cookies from file
if self.args.load_cookies is not None:
if not path.exists(self.args.load_cookies) or not path.isfile(self.args.load_cookies):
self.output.print_and_log('Could not find cookie file: %s' % self.args.load_cookies, logging.ERROR)
exit(-1)
self.cookies = MozillaCookieJar(self.args.load_cookies)
self.cookies.load()
self.session.cookies = self.cookies
# User-Agent
self.ua = UserAgent() if self.args.random_agent else None
示例8: _on_request
# 需要导入模块: from cookies import Cookies [as 别名]
# 或者: from cookies.Cookies import from_request [as 别名]
def _on_request(self, session, request, **kwargs):
match = self._find_match(request)
# TODO(dcramer): find the correct class for this
if match is None:
error_msg = 'Connection refused: {0} {1}'.format(request.method,
request.url)
response = ConnectionError(error_msg)
self._calls.add(request, response)
raise response
if 'body' in match and isinstance(match['body'], Exception):
self._calls.add(request, match['body'])
raise match['body']
headers = {
'Content-Type': match['content_type'],
}
if 'callback' in match: # use callback
status, r_headers, body = match['callback'](request)
if isinstance(body, six.text_type):
body = body.encode('utf-8')
body = BufferIO(body)
headers.update(r_headers)
elif 'body' in match:
if match['adding_headers']:
headers.update(match['adding_headers'])
status = match['status']
body = BufferIO(match['body'])
response = HTTPResponse(
status=status,
body=body,
headers=headers,
preload_content=False,
)
adapter = session.get_adapter(request.url)
response = adapter.build_response(request, response)
if not match.get('stream'):
response.content # NOQA
try:
resp_cookies = Cookies.from_request(response.headers['set-cookie'])
response.cookies = cookiejar_from_dict(dict(
(v.name, v.value)
for _, v
in resp_cookies.items()
))
session.cookies = response.cookies
except (KeyError, TypeError):
pass
self._calls.add(request, response)
if kwargs.get('allow_redirects') and response.is_redirect:
# include redirect resolving logic from requests.sessions.Session
keep_kws = ('stream', 'timeout', 'cert', 'proxies')
resolve_kwargs = dict([(k, v) for (k, v) in kwargs.items() if
k in keep_kws])
# this recurses if response.is_redirect,
# but limited by session.max_redirects
gen = session.resolve_redirects(response, request,
**resolve_kwargs)
history = [resp for resp in gen]
# Shuffle things around if there's history.
if history:
# Insert the first (original) request at the start
history.insert(0, response)
# Get the last request made
response = history.pop()
response.history = history
return response