我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cookielib.CookieJar()。
def __init__(self, ssl_context, debug=False, proxies=None, no_proxy=None, cookie=None, http_basicauth=None, headers=None): """ @param ssl_context: SSL context to use with this configuration @type ssl_context: OpenSSL.SSL.Context @param debug: if True, output debugging information @type debug: bool @param proxies: proxies to use for @type proxies: dict with basestring keys and values @param no_proxy: hosts for which a proxy should not be used @type no_proxy: basestring @param cookie: cookies to set for request @type cookie: cookielib.CookieJar (python 3 - http.cookiejar) @param http_basicauth: http authentication, or None @type http_basicauth: tuple of (username,password) @param headers: http headers @type headers: dict """ self.ssl_context = ssl_context self.debug = debug self.proxies = proxies self.no_proxy = no_proxy self.cookie = cookie self.http_basicauth = http_basicauth self.headers = headers
def logout(self): ''' ???? :returns: True????False???????? ''' #passport_logout_response = self.get_response(logout_url) self.session.cookies = cookielib.CookieJar() response = self.get_response(logout_url) check_logout = re.findall('????', response) if len(check_logout) > 0: self.logined = False self.remove_cookies() return True else: return False
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') handlers = [] if ((sys.version_info[0] == 2 and sys.version_info >= (2,7,9)) or (sys.version_info[0] == 3 and sys.version_info >= (3,2,0))): context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE handlers.append(urllib2.HTTPSHandler(context=context)) if sessions: handlers.append(urllib2.HTTPCookieProcessor(CookieJar())) opener = urllib2.build_opener(*handlers) self.request_opener = opener.open self._timeout = timeout
def register(first_name, last_name, email, password, captcha_fn): cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) html = opener.open(REGISTER_URL).read() form = parse_form(html) form['first_name'] = first_name form['last_name'] = last_name form['email'] = email form['password'] = form['password_two'] = password img = extract_image(html) captcha = captcha_fn(img) form['recaptcha_response_field'] = captcha encoded_data = urllib.urlencode(form) request = urllib2.Request(REGISTER_URL, encoded_data) response = opener.open(request) success = '/user/register' not in response.geturl() return success
def load_ff_sessions(session_filename): cj = cookielib.CookieJar() if os.path.exists(session_filename): try: json_data = json.loads(open(session_filename, 'rb').read()) except ValueError as e: print 'Error parsing session JSON:', str(e) else: for window in json_data.get('windows', []): for cookie in window.get('cookies', []): import pprint; pprint.pprint(cookie) c = cookielib.Cookie(0, cookie.get('name', ''), cookie.get('value', ''), None, False, cookie.get('host', ''), cookie.get('host', '').startswith('.'), cookie.get('host', '').startswith('.'), cookie.get('path', ''), False, False, str(int(time.time()) + 3600 * 24 * 7), False, None, None, {}) cj.set_cookie(c) else: print 'Session filename does not exist:', session_filename return cj
def get_cookie(self,query): """ @summary: ??cookie @param query: ????????? @return: ??cookie?? """ cookies={} i=0 while True: cookie = cookielib.CookieJar() handler=urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) response = opener.open(self._search_url.format(query)) # ???????????????cookie for item in cookie: # ???????????cookie????SNUID????????????? # ???SNUID???cookie????cookie??????????? if("SNUID" in item.name): cookies[item.name]=item.value return cookies if(i>3): # ????3???????????cookie????IP????????????cookie?????????? spider_logger.error("Can't get cookies when searching {0} !".format(query)) return cookies i=i+1 time.sleep(10*random.expovariate(1)) # ?????????????????????????????
def test_secure(self): from cookielib import CookieJar, DefaultCookiePolicy for ns in True, False: for whitespace in " ", "": c = CookieJar() if ns: pol = DefaultCookiePolicy(rfc2965=False) int = interact_netscape vs = "" else: pol = DefaultCookiePolicy(rfc2965=True) int = interact_2965 vs = "; Version=1" c.set_policy(pol) url = "http://www.acme.com/" int(c, url, "foo1=bar%s%s" % (vs, whitespace)) int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace)) self.assertFalse( c._cookies["www.acme.com"]["/"]["foo1"].secure, "non-secure cookie registered secure") self.assertTrue( c._cookies["www.acme.com"]["/"]["foo2"].secure, "secure cookie registered non-secure")
def test_domain_mirror(self): from cookielib import CookieJar, DefaultCookiePolicy pol = DefaultCookiePolicy(rfc2965=True) c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, "spam=eggs; Version=1") h = interact_2965(c, url) self.assertNotIn("Domain", h, "absent domain returned with domain present") c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Domain=.bar.com') h = interact_2965(c, url) self.assertIn('$Domain=".bar.com"', h, "domain not returned") c = CookieJar(pol) url = "http://foo.bar.com/" # note missing initial dot in Domain interact_2965(c, url, 'spam=eggs; Version=1; Domain=bar.com') h = interact_2965(c, url) self.assertIn('$Domain="bar.com"', h, "domain not returned")
def test_path_mirror(self): from cookielib import CookieJar, DefaultCookiePolicy pol = DefaultCookiePolicy(rfc2965=True) c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, "spam=eggs; Version=1") h = interact_2965(c, url) self.assertNotIn("Path", h, "absent path returned with path present") c = CookieJar(pol) url = "http://foo.bar.com/" interact_2965(c, url, 'spam=eggs; Version=1; Path=/') h = interact_2965(c, url) self.assertIn('$Path="/"', h, "path not returned")
def test_url_encoding(self): # Try some URL encodings of the PATHs. # (the behaviour here has changed from libwww-perl) from cookielib import CookieJar, DefaultCookiePolicy c = CookieJar(DefaultCookiePolicy(rfc2965=True)) interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5", "foo = bar; version = 1") cookie = interact_2965( c, "http://www.acme.com/foo%2f%25/<<%0anew/", 'bar=baz; path="/foo/"; version=1'); version_re = re.compile(r'^\$version=\"?1\"?', re.I) self.assertIn("foo=bar", cookie) self.assertRegexpMatches(cookie, version_re) cookie = interact_2965( c, "http://www.acme.com/foo/%25/<<%0anew/") self.assertFalse(cookie) # unicode URL doesn't raise exception cookie = interact_2965(c, u"http://www.acme.com/\xfc")
def cookiejar_from_dict(cookie_dict): """Returns a CookieJar from a key/value dictionary. :param cookie_dict: Dict of key/values to insert into CookieJar. """ # return cookiejar if one was passed in if isinstance(cookie_dict, cookielib.CookieJar): return cookie_dict # create cookiejar cj = cookielib.CookieJar() cj = add_dict_to_cookiejar(cj, cookie_dict) return cj
def __init__(self, user, pwd, softId="110614", softKey="469c0d8a805a40f39d3c1ec3c9281e9c", codeType="1004"): self.softId = softId self.softKey = softKey self.user = user self.pwd = pwd self.codeType = codeType self.uid = "100" self.initUrl = "http://common.taskok.com:9000/Service/ServerConfig.aspx" self.version = '1.1.1.2' self.cookieJar = cookielib.CookieJar() self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cookieJar)) self.loginUrl = None self.uploadUrl = None self.codeUrl = None self.params = [] self.uKey = None
def get_page(url): try: headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebkit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240', 'Connection':'Keep-Alive', 'Accept':'text/html, application/xhtml+xml, image/jxr, */*', 'Accept-Language':'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3', } cookie_jar = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) req = urllib2.Request(url = url, headers = headers) response = opener.open(req, timeout = 5) the_page = response.read() headers = response.info() return the_page, headers except: import traceback traceback.print_exc()
def adminLogin(username, password): print '--- Initializing ---' cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) print '--- Geting Cookie ---' link = urllib2.urlopen('http://www.baidu.com/') print '--- Geting Token ---' token = eval(urllib2.urlopen('https://passport.baidu.com/v2/api/?getapi&tpl=pp&apiver=v3&class=login').read())['data']['token'] print "Token: " + token print '--- Sign In Posting ---' postdata = { 'token' : token, 'tpl' : 'pp', 'username' : username, 'password' : password, } sendRequest('https://passport.baidu.com/v2/api/?login', postdata) link.close() return
def get_url(self, url, headers, cookie): if not isinstance(url, str): raise 'url or cookie type error!!' req = urllib2.Request(url, None, headers) try: if not isinstance(cookie, cookielib.CookieJar) and cookie is None: response = urllib2.urlopen(req) elif not isinstance(cookie, cookielib.CookieJar) and cookie is not None: cookie = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) response = opener.open(req) elif isinstance(cookie, cookielib.CookieJar): handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) response = opener.open(req) except urllib2.HTTPError: raise 'get url error!!' the_page = response.read() return the_page, cookie
def post_url(self, url, formdata, headers, cookie): if not isinstance(url, str): raise 'url must be a string and fordata must be a dict' data = urllib.urlencode(formdata) req = urllib2.Request(url, data, headers) try: if not isinstance(cookie, cookielib.CookieJar) and cookie is None: response = urllib2.urlopen(req) elif not isinstance(cookie, cookielib.CookieJar) and cookie is not None: cookie = cookielib.CookieJar() handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) response = opener.open(req) elif isinstance(cookie, cookielib.CookieJar): handler = urllib2.HTTPCookieProcessor(cookie) opener = urllib2.build_opener(handler) response = opener.open(req) except urllib2.HTTPError: raise 'get url error!!' the_page = response.read() return the_page, cookie
def _init_urllib(self, secure, debuglevel=0): cj = cookielib.CookieJar() no_proxy_support = urllib2.ProxyHandler({}) cookie_handler = urllib2.HTTPCookieProcessor(cj) ctx = None if not secure: self._logger.info('[WARNING] Skip certificate verification.') ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE https_handler = urllib2.HTTPSHandler(debuglevel=debuglevel, context=ctx) opener = urllib2.build_opener(no_proxy_support, cookie_handler, https_handler, MultipartPostHandler.MultipartPostHandler) opener.addheaders = [('User-agent', API_USER_AGENT)] urllib2.install_opener(opener)
def verify(cls, args): cookie = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) urllib2.install_opener(opener) postdata = "_SESSION[login_in]=1&_SESSION[admin]=1&_SESSION[login_time]=300000000000000000000000\r\n" # get session request = urllib2.Request(args['options']['target'] + "/index.php", data=postdata) r = urllib2.urlopen(request) # login test request2 = urllib2.Request(args['options']['target'] + "/admin/admin.php", data=postdata) r = urllib2.urlopen(request2) content = r.read() if "admin_form.php?action=form_list&nav=list_order" in content: if "admin_main.php?nav=main" in content: args['success'] = True args['test_method'] = 'http://www.wooyun.org/bugs/wooyun-2014-059180' return args args['success'] = False return args
def index(request): if request.method == "GET": try: ssl._create_default_https_context = ssl._create_unverified_context opener = wdf_urllib.build_opener( wdf_urllib.HTTPCookieProcessor(CookieJar())) wdf_urllib.install_opener(opener) except: pass uuid = getUUID() url = 'https://login.weixin.qq.com/qrcode/' + uuid params = { 't': 'webwx', '_': int(time.time()), } request = getRequest(url=url, data=urlencode(params)) response = wdf_urllib.urlopen(request) context = { 'uuid': uuid, 'response': response.read(), 'delyou': '', } return render_to_response('index.html', context)
def proxy4_login(self, pt): """ Check proxy ticket for service Use a new opener so its not got any cookies / auth already """ opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookielib.CookieJar())) url_args = (self.urls['cas'], self.urls['app'], pt) url = '%sproxyValidate?service=%s&ticket=%s' % url_args try: login = opener.open(url) except: return 'FAIL: PTURL=%s not found' % url page = login.read() print page if page.find('cas:authenticationSuccess') > -1: proxy = self.find_in_dom(page,['cas:proxies', 'cas:proxy']) return proxy return None
def test_url_encoding(self): # Try some URL encodings of the PATHs. # (the behaviour here has changed from libwww-perl) from cookielib import CookieJar, DefaultCookiePolicy c = CookieJar(DefaultCookiePolicy(rfc2965=True)) interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5", "foo = bar; version = 1") cookie = interact_2965( c, "http://www.acme.com/foo%2f%25/<<%0anewå/æøå", 'bar=baz; path="/foo/"; version=1'); version_re = re.compile(r'^\$version=\"?1\"?', re.I) self.assertIn("foo=bar", cookie) self.assertRegexpMatches(cookie, version_re) cookie = interact_2965( c, "http://www.acme.com/foo/%25/<<%0anewå/æøå") self.assertFalse(cookie) # unicode URL doesn't raise exception cookie = interact_2965(c, u"http://www.acme.com/\xfc")
def test_secure(self): from cookielib import CookieJar, DefaultCookiePolicy for ns in True, False: for whitespace in " ", "": c = CookieJar() if ns: pol = DefaultCookiePolicy(rfc2965=False) int = interact_netscape vs = "" else: pol = DefaultCookiePolicy(rfc2965=True) int = interact_2965 vs = "; Version=1" c.set_policy(pol) url = "http://www.acme.com/" int(c, url, "foo1=bar%s%s" % (vs, whitespace)) int(c, url, "foo2=bar%s; secure%s" % (vs, whitespace)) self.assertTrue( not c._cookies["www.acme.com"]["/"]["foo1"].secure, "non-secure cookie registered secure") self.assertTrue( c._cookies["www.acme.com"]["/"]["foo2"].secure, "secure cookie registered non-secure")
def test_url_encoding(self): # Try some URL encodings of the PATHs. # (the behaviour here has changed from libwww-perl) from cookielib import CookieJar, DefaultCookiePolicy c = CookieJar(DefaultCookiePolicy(rfc2965=True)) interact_2965(c, "http://www.acme.com/foo%2f%25/%3c%3c%0Anew%E5/%E5", "foo = bar; version = 1") cookie = interact_2965( c, "http://www.acme.com/foo%2f%25/<<%0anew/", 'bar=baz; path="/foo/"; version=1'); version_re = re.compile(r'^\$version=\"?1\"?', re.I) self.assertTrue("foo=bar" in cookie and version_re.search(cookie)) cookie = interact_2965( c, "http://www.acme.com/foo/%25/<<%0anew/") self.assertTrue(not cookie) # unicode URL doesn't raise exception cookie = interact_2965(c, u"http://www.acme.com/\xfc")
def getPage(self, url, postdata=None, headers=None): try: cookie = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) if postdata is None: if headers is None: request = urllib2.Request(url) else: request = urllib2.Request(url, headers=headers) else: if headers is not None: request = urllib2.Request(url, postdata, headers) page = opener.open(request) redirect_url = opener.open(request).geturl() # ?????url soup = BeautifulSoup(page, 'html.parser') return page, soup, redirect_url # ??????? except urllib2.URLError, e: if hasattr(e, "reason"): print u"????,????", e.reason return None # ?????
def __init__(self, cookiejar=None): import cookielib if cookiejar is None: cookiejar = cookielib.CookieJar() self.cookiejar = cookiejar
def load(self): '''Load cookies into a cookiejar''' cookie_jar = cookielib.CookieJar() for cookie in self.get_cookies(): cookie_jar.set_cookie(cookie) return cookie_jar
def load(): """Try to load cookies from all supported browsers and return combined cookiejar """ cookie_jar = cookielib.CookieJar() for cookie in sorted(_get_cookies(), key=lambda cookie: cookie.expires): cookie_jar.set_cookie(cookie) return cookie_jar
def installCookie(self): if self.__opener is None: self.__cookies = cookielib.CookieJar() proxy_support= None if self.__proxyIp is not None: proxy_support = urllib.request.ProxyHandler(self.__proxyIp) self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies),proxy_support) else: self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies)) urllib2.install_opener(self.__opener)
def installCookie(self): if self.__opener is None: self.__cookies = cookielib.CookieJar() self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.__cookies)) urllib2.install_opener(self.__opener)
def postmsg(): myname = raw_input('Name : ') mymsg = raw_input('Message :') submitvalue = "submit" myidcode = getClientCode() cjar = cookielib.CookieJar() myopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cjar)) user_agent = 'Mozilla/4.0 (compatible, MSIE 5.5; Windows NT)' headers = {'User-Agent': user_agent} myvalues = [ ('name', myname), ('message', mymsg), ('code', myidcode), ('shout', submitvalue) ] fencode = urllib.urlencode(myvalues) rqdata = urllib2.Request('http://'+realhost+'.shoutmix.com/?'+options.smuser+"=process", fencode, headers) respdata = urllib2.urlopen(rqdata).read() print "string sent"
def __init__(self, login, password): self.__username = login self.__password = password # Stworzenie s?oika na ciasteczka ;) self.__cj = cj = cookielib.CookieJar() self.__opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) self.__login()
def __init__(self, postdata, url): threading.Thread.__init__(self) self.setDaemon(True) self.url = url self.postdata = postdata self.cookie = cookielib.CookieJar() self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
def __init__(self, url): threading.Thread.__init__(self) self.setDaemon(True) self.url = url self.cookie = cookielib.CookieJar() self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
def build_opener_with_chrome_cookies(domain=None): #????? cookie_file_path = os.path.join(os.environ['LOCALAPPDATA'], r'Google\Chrome\User Data\Default\Cookies') if not os.path.exists(cookie_file_path): raise Exception('Cookies file not exist!') conn = sqlite3.connect(cookie_file_path) sql = 'select host_key, name, value, path from cookies' if domain: sql += ' where host_key like "%{}%"'.format(domain) cookiejar = cookielib.CookieJar() # No cookies stored yet for row in conn.execute(sql): print row cookie_item = cookielib.Cookie( version=0, name=row[1], value=row[2], port=None, port_specified=None, domain=row[0], domain_specified=None, domain_initial_dot=None, path=row[3], path_specified=None, secure=None, expires=None, discard=None, comment=None, comment_url=None, rest=None, rfc2109=False, ) cookiejar.set_cookie(cookie_item) # Apply each cookie_item to cookiejar conn.close() return urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar)) # Return opener #build_opener_with_chrome_cookies()
def cookieAgentFactory(verify_path, connectTimeout=30): customPolicy = BrowserLikePolicyForHTTPS( Certificate.loadPEM(FilePath(verify_path).getContent())) agent = Agent(reactor, customPolicy, connectTimeout=connectTimeout) cookiejar = cookielib.CookieJar() return CookieAgent(agent, cookiejar)
def upgradeWiseproxy(proxys): '''?? ????wiseproxy ????wiseproxy''' dir_r = os.path.dirname(os.path.abspath(__file__)) etc_c = os.path.join(dir_r, 'wiseproxy.conf') print etc_c cf = ConfigParser.ConfigParser() cf.read(etc_c) login_url = cf.get('main', 'login_url') api_url = cf.get('main', 'api_url') usr_pwd_data = eval(cf.get('main', 'usr_pwd_data')) headers = {'Content-type': 'application/x-www-form-urlencoded', 'charset': 'utf-8'} # login cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) req = urllib2.Request(login_url, urlencode(usr_pwd_data), headers) opener.open(req) #delete old proxy queryurl='http://wiseproxy.ops.vobile.org/getProxyList?partitionSearch=buy&proxySearch=&_search=false&nd=1503380267574&rows=500&page=1&sidx=&sord=asc' #http://wiseproxy.ops.vobile.org/getProxyList?partitionSearch=&proxySearch=120.55.115.209:63267&_search=false&nd=1503563948157&rows=10&page=1&sidx=&sord=asc oldproxys = opener.open(queryurl).read() oldproxys=json.loads(oldproxys) oldproxylist=oldproxys['rows'] for oldproxy in oldproxylist: proxyid = oldproxy['id'] print proxyid sys.exit() if type(proxys)==type([]): # addproxy for proxy in proxys: data = {"partition": "9509", "proxyId": -1, "proxyServerInfo": proxy, "source": "Vultr"} request = urllib2.Request(api_url, urlencode(data), headers) response = opener.open(request) else: data = {"partition": "9509", "proxyId": -1, "proxyServerInfo": proxys, "source": "Vultr"} request = urllib2.Request(api_url, urlencode(data), headers) response = opener.open(request) opener.close() #upgradeWiseproxy('192.168.200.252:3127')