def __init__(self):
super(WebService, self).__init__()
self.cache = defaultdict(defaultdict)
self._cookie = CookieJar()
self._opener = urllib2.build_opener(
urllib2.HTTPCookieProcessor(self._cookie))
self.query_interval = 1
python类CookieJar()的实例源码
def test_cookie_redirect(self):
# cookies shouldn't leak into redirected requests
from http.cookiejar import CookieJar
from test.test_http_cookiejar import interact_netscape
cj = CookieJar()
interact_netscape(cj, "http://www.example.com/", "spam=eggs")
hh = MockHTTPHandler(302, "Location: http://www.cracker.com/\r\n\r\n")
hdeh = urllib.request.HTTPDefaultErrorHandler()
hrh = urllib.request.HTTPRedirectHandler()
cp = urllib.request.HTTPCookieProcessor(cj)
o = build_test_opener(hh, hdeh, hrh, cp)
o.open("http://www.example.com/")
self.assertFalse(hh.req.has_header("Cookie"))
def from_url(cls, url, tokenizer):
headers = {
'User-Agent': ' '.join([
'Mozilla/5.0 (X11; Linux x86_64)',
'AppleWebKit/537.11 (KHTML, like Gecko)',
'Chrome/23.0.1271.64 Safari/537.11'
]),
'Accept': ','.join([
'text/html', 'application/xhtml+xml', 'application/xml;q=0.9',
'*/*;q=0.8'
]),
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'
}
session = Session()
session.mount('http://', HTTPAdapter(max_retries=2))
session.mount('https://', HTTPAdapter(max_retries=2))
cookies = CookieJar()
request = Request(method='GET',
url=url,
headers=headers,
cookies=cookies)
prepare = session.prepare_request(request)
response = session.send(prepare, verify=True)
if response.status_code != requests.codes.ok:
response.raise_for_status()
return cls(response.text, tokenizer, url)
def __init__(self, begin_date_str):
self.__base_url = "http://xk.autoisp.shu.edu.cn"
monday = datetime.strptime(begin_date_str, '%Y.%m.%d').date()
tuesday = monday + timedelta(days=1)
wednesday = tuesday + timedelta(days=1)
thursday = wednesday + timedelta(days=1)
friday = thursday + timedelta(days=1)
self.__weekday_table = {'?': monday, '?': tuesday, '?': wednesday, '?': thursday, '?': friday}
self.__course_time_table = (time(hour=8, minute=0, tzinfo=timezone("Asia/Shanghai")),
time(hour=8, minute=55, tzinfo=timezone("Asia/Shanghai")),
time(hour=10, minute=0, tzinfo=timezone("Asia/Shanghai")),
time(hour=10, minute=55, tzinfo=timezone("Asia/Shanghai")),
time(hour=12, minute=10, tzinfo=timezone("Asia/Shanghai")),
time(hour=13, minute=5, tzinfo=timezone("Asia/Shanghai")),
time(hour=14, minute=10, tzinfo=timezone("Asia/Shanghai")),
time(hour=15, minute=5, tzinfo=timezone("Asia/Shanghai")),
time(hour=16, minute=0, tzinfo=timezone("Asia/Shanghai")),
time(hour=16, minute=55, tzinfo=timezone("Asia/Shanghai")),
time(hour=18, minute=0, tzinfo=timezone("Asia/Shanghai")),
time(hour=18, minute=55, tzinfo=timezone("Asia/Shanghai")),
time(hour=19, minute=50, tzinfo=timezone("Asia/Shanghai")))
# Create a opener with the ability to record cookies.
self.__opener = build_opener(HTTPCookieProcessor(CookieJar()))
self.__validate_img_path = os.path.join(os.getcwd(), "validate_code.jpg")
self.term_index = 0
self.terms_and_ports = []
ports = (80, 8080)
for port in ports:
url = '%s:%d' % (self.__base_url, port)
data = self.__opener.open(url).read().decode('utf-8')
term = re.search(pattern=r'<div style="color: Red; font-size: 26px; text-align: center;">(.+?)</div>',
string=data).group(1)
self.terms_and_ports.append((term, port))
def test_app_secure_cookies():
cookies_view.set_secure_cookie('test', '????')
cookies_view.set_secure_cookie('test2', {'value': '????'})
cookies_view.finish(RETCODE.SUCCESS)
cookies_jar = CookieJar()
for k, v in cookies_view.response.cookies.items():
cookies_jar.set_cookie(morsel_to_cookie(v))
cookies_view._request.cookies = dict_from_cookiejar(cookies_jar)
assert cookies_view.get_secure_cookie('test') == '????'
assert cookies_view.get_secure_cookie('test2') == {'value': '????'}
def authenticate(username,passwd,login=False):
"""
??????login?True,????ret_val.data.opener???opener
:param username:
:param passwd:
:param login:
:return:
"""
postdata = parse.urlencode({
"txtUserID": username,
"txtUserPwd": passwd,
"btnLogon": "??",
'__VIEWSTATE': '/wEPDwUKMTM1MzI1Njg5N2Rk47x7/EAaT/4MwkLGxreXh8mHHxA=',
'__EVENTVALIDATION': '/wEWBAKo25zdBALT8dy8BQLG8eCkDwKk07qFCRXt1F3RFYVdjuYasktKIhLnziqd', #aspx?????
}).encode('utf-8')
if not login:
try:
resp = request.urlopen(login_host,postdata,timeout=default_timeout)
except Exception as e:
return error(Error.CONNECT_ERROR.value)
if resp.read().__contains__(b'alert'):
return failed('????')
else:
return success('????')
else:
cookie_jar = cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cookie_jar))
try:
resp = opener.open(login_host,postdata,default_timeout)
except Exception as e:
return error(Error.CONNECT_ERROR.value)
if resp.read().__contains__(b'alert'):
return failed('????')
else:
return success('????',opener=opener)
def __init__(self, timeout=None, proxy=None, cacert=None, sessions=False):
if (timeout is not None) and not self.supports_feature('timeout'):
raise RuntimeError('timeout is not supported with urllib2 transport')
if proxy:
raise RuntimeError('proxy is not supported with urllib2 transport')
if cacert:
raise RuntimeError('cacert is not support with urllib2 transport')
self.request_opener = urllib2.urlopen
if sessions:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
self.request_opener = opener.open
self._timeout = timeout
def __init__(self, username=None, password=None, pw_hash=None,
pw_session_hash=None, session_id=None):
if not ((username and password) or session_id):
raise TypeError("not enough parameters")
self.__cj = CookieJar()
self.__opener = build_opener(
HTTPCookieProcessor(self.__cj))
self.__opener.addheaders = [
('User-Agent',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100102'
' Firefox/46.0)')
]
self.username = username
self.password = password
self.pw_hash = pw_hash
self.pw_session_hash = pw_session_hash
self.session_id = None
if not username and not (password or pw_hash or
pw_session_hash) and session_id:
self.set_sessid(session_id)
return
response = self.__opener.open("http://dplogin.com/index.php",
data=urlencode({"action": "weblogin1",
"username": username,
"pwhash": ""}).encode('utf-8')).read().decode('utf-8')
try:
self.user_id = PATTERN_USER_ID.findall(response)[0]
except IndexError:
raise TypeError("User {} doesn't exist.".format(username))
self.session_id = None
if not session_id:
for cookie in self.__cj:
if cookie.name == "PHPSESSID":
self.set_sessid(cookie.value)
else:
self.set_sessid(session_id)
pwhash = None
if password:
pwhash = get_password_hash(password, self.user_id, self.session_id)
elif pw_hash:
pwhash = get_session_hash(pw_hash, session_id=session_id)
elif pw_session_hash:
pwhash = pw_session_hash
response = self.__opener.open("http://dplogin.com/index.php",
data=urlencode({"action": "weblogin2",
"username": username,
"pwhash": pwhash,
"password": ""}).encode('utf-8')).read().decode('utf-8')
if "Invalid password" in response:
raise TypeError("Wrong password.")
def baidu_pan_protected_share(url):
print('This share is protected by password!')
inpwd = input('Please provide unlock password: ')
inpwd = inpwd.replace(' ', '').replace('\t', '')
print('Please wait...')
post_pwd = {
'pwd': inpwd,
'vcode': None,
'vstr': None
}
from http import cookiejar
import time
cookiejar = cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cookiejar))
resp = opener.open('http://pan.baidu.com')
resp = opener.open(url)
init_url = resp.geturl()
verify_url = 'http://pan.baidu.com/share/verify?%s&t=%s&channel=chunlei&clienttype=0&web=1' % (
init_url.split('?', 1)[1], int(time.time()))
refer_url = init_url
fake_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'UTF-8,*;q=0.5',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Host': 'pan.baidu.com',
'Origin': 'http://pan.baidu.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:13.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2500.0 Safari/537.36',
'Referer': refer_url
}
opener.addheaders = dict2triplet(fake_headers)
pwd_resp = opener.open(verify_url, bytes(
parse.urlencode(post_pwd), 'utf-8'))
pwd_resp_str = ungzip(pwd_resp.read()).decode('utf-8')
pwd_res = json.loads(pwd_resp_str)
if pwd_res['errno'] != 0:
raise AssertionError(
'Server returned an error: %s (Incorrect password?)' % pwd_res['errno'])
pg_resp = opener.open('http://pan.baidu.com/share/link?%s' %
init_url.split('?', 1)[1])
content = ungzip(pg_resp.read()).decode('utf-8')
sign, timestamp, bdstoken, appid, primary_id, fs_id, uk = baidu_pan_parse(
content)
psk = query_cookiejar(cookiejar, 'BDCLND')
psk = parse.unquote(psk)
fake_headers['Cookie'] = cookjar2hdr(cookiejar)
return sign, timestamp, bdstoken, appid, primary_id, fs_id, uk, fake_headers, psk
def baidu_pan_protected_share(url):
print('This share is protected by password!')
inpwd = input('Please provide unlock password: ')
inpwd = inpwd.replace(' ', '').replace('\t', '')
print('Please wait...')
post_pwd = {
'pwd': inpwd,
'vcode': None,
'vstr': None
}
from http import cookiejar
import time
cookiejar = cookiejar.CookieJar()
opener = request.build_opener(request.HTTPCookieProcessor(cookiejar))
resp = opener.open('http://pan.baidu.com')
resp = opener.open(url)
init_url = resp.geturl()
verify_url = 'http://pan.baidu.com/share/verify?%s&t=%s&channel=chunlei&clienttype=0&web=1' % (
init_url.split('?', 1)[1], int(time.time()))
refer_url = init_url
fake_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'UTF-8,*;q=0.5',
'Accept-Encoding': 'gzip,deflate,sdch',
'Accept-Language': 'en-US,en;q=0.8',
'Host': 'pan.baidu.com',
'Origin': 'http://pan.baidu.com',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:13.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2500.0 Safari/537.36',
'Referer': refer_url
}
opener.addheaders = dict2triplet(fake_headers)
pwd_resp = opener.open(verify_url, bytes(
parse.urlencode(post_pwd), 'utf-8'))
pwd_resp_str = ungzip(pwd_resp.read()).decode('utf-8')
pwd_res = json.loads(pwd_resp_str)
if pwd_res['errno'] != 0:
raise AssertionError(
'Server returned an error: %s (Incorrect password?)' % pwd_res['errno'])
pg_resp = opener.open('http://pan.baidu.com/share/link?%s' %
init_url.split('?', 1)[1])
content = ungzip(pg_resp.read()).decode('utf-8')
sign, timestamp, bdstoken, appid, primary_id, fs_id, uk = baidu_pan_parse(
content)
psk = query_cookiejar(cookiejar, 'BDCLND')
psk = parse.unquote(psk)
fake_headers['Cookie'] = cookjar2hdr(cookiejar)
return sign, timestamp, bdstoken, appid, primary_id, fs_id, uk, fake_headers, psk