本文整理汇总了Python中http.cookiejar.CookieJar.extract_cookies方法的典型用法代码示例。如果您正苦于以下问题:Python CookieJar.extract_cookies方法的具体用法?Python CookieJar.extract_cookies怎么用?Python CookieJar.extract_cookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类http.cookiejar.CookieJar
的用法示例。
在下文中一共展示了CookieJar.extract_cookies方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_length
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
def test_length(self):
cookie_jar = CookieJar()
policy = DeFactoCookiePolicy(cookie_jar=cookie_jar)
cookie_jar.set_policy(policy)
request = urllib.request.Request('http://example.com/')
response = FakeResponse(
[
'Set-Cookie: k={0}'.format('a' * 400)
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
print(cookie_jar._cookies)
self.assertTrue(cookie_jar._cookies['example.com']['/'].get('k'))
request = urllib.request.Request('http://example.com/')
response = FakeResponse(
[
'Set-Cookie: k={0}'.format('a' * 5000)
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
self.assertFalse(cookie_jar._cookies['example.com']['/'].get('k2'))
示例2: test_empty_value
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
def test_empty_value(self):
cookie_jar = CookieJar()
policy = DeFactoCookiePolicy(cookie_jar=cookie_jar)
cookie_jar.set_policy(policy)
request = urllib.request.Request('http://example.com/')
response = FakeResponse(
[
'Set-Cookie: k'
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
print(cookie_jar._cookies)
self.assertTrue(cookie_jar._cookies.get('example.com'))
示例3: test_ascii
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
def test_ascii(self):
# Differences with FakeResponse:
# On Python 3, MIME encoded-word syntax is used
# On Python 2, U backslash syntax is used but it's not decoded back.
cookie_jar = CookieJar()
policy = DeFactoCookiePolicy(cookie_jar=cookie_jar)
cookie_jar.set_policy(policy)
request = urllib.request.Request('http://example.com/')
response = FakeResponse(
[
'Set-Cookie: k=ðŸ�'
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
print(cookie_jar._cookies)
self.assertFalse(cookie_jar._cookies.get('example.com'))
示例4: test_domain_limit
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
def test_domain_limit(self):
cookie_jar = CookieJar()
policy = DeFactoCookiePolicy(cookie_jar=cookie_jar)
cookie_jar.set_policy(policy)
request = urllib.request.Request('http://example.com/')
for key in range(55):
response = FakeResponse(
[
'Set-Cookie: k{0}=a'.format(key)
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
if key < 50:
self.assertTrue(
cookie_jar._cookies['example.com']['/']
.get('k{0}'.format(key))
)
else:
self.assertFalse(
cookie_jar._cookies['example.com']['/']
.get('k{0}'.format(key))
)
response = FakeResponse(
[
'Set-Cookie: k3=b'
],
'http://example.com/'
)
cookie_jar.extract_cookies(response, request)
self.assertEqual(
'b',
cookie_jar._cookies['example.com']['/']['k3'].value
)
示例5: HttpTransport
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
class HttpTransport(Transport):
"""
HTTP transport using urllib2. Provided basic http transport
that provides for cookies, proxies but no authentication.
"""
def __init__(self, **kwargs):
"""
@param kwargs: Keyword arguments.
- B{proxy} - An http proxy to be specified on requests.
The proxy is defined as {protocol:proxy,}
- type: I{dict}
- default: {}
- B{timeout} - Set the url open timeout (seconds).
- type: I{float}
- default: 90
"""
Transport.__init__(self)
Unskin(self.options).update(kwargs)
self.cookiejar = CookieJar()
self.proxy = {}
self.urlopener = None
def open(self, request):
try:
url = request.url
log.debug('opening (%s)', url)
u2request = u2.Request(url)
self.proxy = self.options.proxy
return self.u2open(u2request)
except HTTPError as e:
raise TransportError(str(e), e.code, e.fp)
def send(self, request):
result = None
url = request.url
msg = request.message
headers = request.headers
try:
u2request = u2.Request(url, msg, headers)
self.addcookies(u2request)
self.proxy = self.options.proxy
request.headers.update(u2request.headers)
log.debug('sending:\n%s', request)
fp = self.u2open(u2request)
self.getcookies(fp, u2request)
#result = Reply(200, fp.headers.dict, fp.read())
#print(str(fp))
result = Reply(200, fp.headers, fp.read())
log.debug('received:\n%s', result)
except HTTPError as e:
if e.code in (202,204):
result = None
else:
raise TransportError(e.msg, e.code, e.fp)
return result
def addcookies(self, u2request):
"""
Add cookies in the cookiejar to the request.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Requet.
"""
self.cookiejar.add_cookie_header(u2request)
def getcookies(self, fp, u2request):
"""
Add cookies in the request to the cookiejar.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Requet.
"""
self.cookiejar.extract_cookies(fp, u2request)
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Requet.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = self.u2opener()
if self.u2ver() < 2.6:
socket.setdefaulttimeout(tm)
return url.open(u2request)
else:
return url.open(u2request, timeout=tm)
def u2opener(self):
"""
Create a urllib opener.
@return: An opener.
@rtype: I{OpenerDirector}
"""
if self.urlopener is None:
return u2.build_opener(*self.u2handlers())
else:
return self.urlopener
#.........这里部分代码省略.........
示例6: HttpTransport
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
class HttpTransport(Transport):
"""
Basic HTTP transport implemented using using urllib2, that provides for
cookies & proxies but no authentication.
"""
def __init__(self, **kwargs):
"""
@param kwargs: Keyword arguments.
- B{proxy} - An HTTP proxy to be specified on requests.
The proxy is defined as {protocol:proxy,}
- type: I{dict}
- default: {}
- B{timeout} - Set the URL open timeout (seconds).
- type: I{float}
- default: 90
"""
Transport.__init__(self)
Unskin(self.options).update(kwargs)
self.cookiejar = CookieJar()
self.proxy = {}
self.urlopener = None
def open(self, request):
try:
url = self.__get_request_url_for_urllib(request)
log.debug('opening (%s)', url)
u2request = urllib.request.Request(url)
self.proxy = self.options.proxy
return self.u2open(u2request)
except urllib.error.HTTPError as e:
raise TransportError(str(e), e.code, e.fp)
def send(self, request):
url = self.__get_request_url_for_urllib(request)
msg = request.message
headers = request.headers
try:
u2request = urllib.request.Request(url, msg, headers)
self.addcookies(u2request)
self.proxy = self.options.proxy
request.headers.update(u2request.headers)
log.debug('sending:\n%s', request)
fp = self.u2open(u2request)
self.getcookies(fp, u2request)
headers = fp.headers
if sys.version_info < (3, 0):
headers = headers.dict
reply = Reply(http.client.OK, headers, fp.read())
log.debug('received:\n%s', reply)
return reply
except urllib.error.HTTPError as e:
if e.code not in (http.client.ACCEPTED, http.client.NO_CONTENT):
raise TransportError(e.msg, e.code, e.fp)
def addcookies(self, u2request):
"""
Add cookies in the cookiejar to the request.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Request.
"""
self.cookiejar.add_cookie_header(u2request)
def getcookies(self, fp, u2request):
"""
Add cookies in the request to the cookiejar.
@param u2request: A urllib2 request.
@rtype: u2request: urllib2.Request.
"""
self.cookiejar.extract_cookies(fp, u2request)
def u2open(self, u2request):
"""
Open a connection.
@param u2request: A urllib2 request.
@type u2request: urllib2.Request.
@return: The opened file-like urllib2 object.
@rtype: fp
"""
tm = self.options.timeout
url = self.u2opener()
if (sys.version_info < (3, 0)) and (self.u2ver() < 2.6):
socket.setdefaulttimeout(tm)
return url.open(u2request)
return url.open(u2request, timeout=tm)
def u2opener(self):
"""
Create a urllib opener.
@return: An opener.
@rtype: I{OpenerDirector}
#.........这里部分代码省略.........
示例7: register
# 需要导入模块: from http.cookiejar import CookieJar [as 别名]
# 或者: from http.cookiejar.CookieJar import extract_cookies [as 别名]
def register(self):
if not (any(self.user.breakfast) or any(self.user.lunch) or any(self.user.dinner)):
print("\tNothing to reserve.")
return None
try:
display = Display(visible=False, size=(1600, 1200))
display.start()
with contextlib.closing(webdriver.Firefox()) as browser:
cj = CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
authentication_url = "https://stu.iust.ac.ir/j_security_check"
payload = {"j_username": self.user.stu_username,
"j_password": self.user.stu_password,
"captcha_input": get_captcha(cj),
"login": u"ورود", }
data = urllib.parse.urlencode(payload)
binary_data = data.encode('UTF-8')
request = urllib.request.Request(authentication_url, binary_data)
response = opener.open(request)
cj.extract_cookies(response, request)
print(cj._cookies)
contents = str(response.read(), 'utf-8')
if u'iconWarning.gif' in contents:
print("wrong user pass")
return "wup"
new_cookie = {'expiry': None, 'value':cj._cookies['stu.iust.ac.ir']['/']['JSESSIONID'].value, 'name': 'JSESSIONID', 'secure': True, 'path': '/', 'domain': 'stu.iust.ac.ir'}
#browser = webdriver.Firefox()
browser.get("https://stu.iust.ac.ir")
browser.add_cookie(new_cookie)
# TODO: handle wrong user or pass
browser.get("https://stu.iust.ac.ir/nurture/user/multi/reserve/showPanel.rose")
browser.find_element_by_id("nextWeekBtn").click()
import time
for self_id in set(self.user.breakfast + self.user.lunch + self.user.dinner) - {0}:
# browser.get("https://stu.iust.ac.ir/nurture/user/multi/reserve/showPanel.rose")
self_hidden_id = None
for i in range(10):
try:
self_hidden_id = browser.find_element_by_id("selfHiddenId")
break
except:
time.sleep(0.3)
if self_hidden_id.get_attribute('value') != self_id:
try:
browser.find_element_by_id("selfId").find_element_by_xpath(
"//option[@value='" + str(self_id) + "']").click()
except NoSuchElementException:
print("\tERR - Invalid self: {} self:{}".format(self.user.stu_username, self_id))
continue
foods_to_register = []
food_chart = _get_foods(browser.page_source)
for i, day in enumerate(self.user.breakfast):
if day == self_id:
foods_in_day = food_chart[i][0]
chosen = choose_food(self.user, foods_in_day)
if chosen is not None:
foods_to_register.append(chosen)
for i, day in enumerate(self.user.lunch):
if day == self_id:
foods_in_day = food_chart[i][1]
chosen = choose_food(self.user, foods_in_day)
if chosen is not None:
foods_to_register.append(chosen)
for i, day in enumerate(self.user.dinner):
if day == self_id:
foods_in_day = food_chart[i][2]
chosen = choose_food(self.user, foods_in_day)
if chosen is not None:
foods_to_register.append(chosen)
for index, food_to_check in foods_to_register:
print("\t->" + str(food_to_check))
browser.find_element_by_id("userWeekReserves.selected" + str(index)).click()
browser.find_element_by_id("doReservBtn").click()
except Exception as e:
return str(e)
# browser.quit()
display.stop()
return None