我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用cookielib.LWPCookieJar()。
def login(i,j,ur): ua = UserAgent() cookiejar =cookielib.LWPCookieJar() br = mechanize.Browser() br.set_cookiejar(cookiejar) #br.set_debug_http(True) br.set_handle_robots(False) br.set_handle_refresh(False) br.addheaders = [('User-Agent', ua.random), ('Accept', '*/*')] url = "http://www."+ur+"/" br.open(url) br.select_form(nr = 1) br.form['username'] = i br.form['password'] = j br.submit() if len(br.geturl()) == 72: return True else: return False
def __init__(self): lf.input_account() self._url = gl.g_zhihu_host self.account = gl.g_zhihu_account self._verify_code = "" self.default_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0", "connection":"keep-alive"} self._is_init = False self.login_success = False self.logger = ZhihuLog.creatlogger(self.__class__.__name__) self.session = requests.Session() self.session.headers = self.default_headers self.session.cookies = cookielib.LWPCookieJar(gl.g_config_folder + 'cookiejar') #if os.path.exists(gl.g_config_folder+'cookiejar'): # self.session.cookies.load(ignore_discard=True) #self.login_success = True if not self.login_success : self.login() pass
def get_cookies(): """???? cookies, ?????.""" headers = { "User-Agent": ( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36" "(KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36" ) } save_cookies_file = 'cookies.txt' jar = cookielib.LWPCookieJar(save_cookies_file) sess = requests.session() sess.headers = headers sess.cookies = jar sess.get('http://tieba.baidu.com/') jar.save(ignore_expires=True, ignore_discard=True) return jar
def __init__(self): self.cookieJar = cookielib.LWPCookieJar() self.session = None self.clientHeader = None self.average_download_speed = 0.0 self.manifest_playlist = None self.media_list = None self.selected_bandwidth_index = None self.use_proxy = False self.proxy = None self.g_stopEvent = None self.maxbitrate = 0 self.url = None self.media_buffer = {} self.requested_segment = None self.key = None self.queue = None
def test_bad_magic(self): from cookielib import LWPCookieJar, MozillaCookieJar, LoadError # IOErrors (eg. file doesn't exist) are allowed to propagate filename = test_support.TESTFN for cookiejar_class in LWPCookieJar, MozillaCookieJar: c = cookiejar_class() try: c.load(filename="for this test to work, a file with this " "filename should not exist") except IOError, exc: # exactly IOError, not LoadError self.assertEqual(exc.__class__, IOError) else: self.fail("expected IOError for invalid filename") # Invalid contents of cookies file (eg. bad magic string) # causes a LoadError. try: f = open(filename, "w") f.write("oops\n") for cookiejar_class in LWPCookieJar, MozillaCookieJar: c = cookiejar_class() self.assertRaises(LoadError, c.load, filename) finally: try: os.unlink(filename) except OSError: pass
def set_cookie(cookie_file): """ @brief Load cookie from file @param cookie_file @param user_agent @return cookie, LWPCookieJar """ cookie = cookielib.LWPCookieJar(cookie_file) try: cookie.load(ignore_discard=True) except: Log.error(traceback.format_exc()) opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie)) opener.addheaders = Constant.HTTP_HEADER_USERAGENT urllib2.install_opener(opener) return cookie
def __init__(self, headers = {}, debug = True, p = ''): self.timeout = 10 self.br = mechanize.Browser() #???br self.cj = cookielib.LWPCookieJar() self.br.set_cookiejar(self.cj)#??cookie self.br.set_handle_equiv(True)#????http equiv self.br.set_handle_gzip(True)#?????? self.br.set_handle_redirect(True)#??????? self.br.set_handle_referer(True)#??????referer self.br.set_handle_robots(False)#????robots?? self.br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) self.br.addheaders = [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36')] self.debug = debug #debug????????????? if self.debug: self.br.set_debug_http(True) self.br.set_debug_redirects(True) self.br.set_debug_responses(True) #headers for keys in headers.keys(): self.br.addheaders += [(key, headers[key]), ] #proxy if len(p) > 0 and p != 'None' and p != None and p != 'NULL': self.br.set_proxies({'http': p})
def parse(self, response): topic_xpath_rule = '//li[@class="zm-topic-cat-item"]/a/text()' topic_names = response.selector.xpath(topic_xpath_rule).extract() topic_xpath_rule = '//li[@class="zm-topic-cat-item"]/@data-id' topic_ids = response.selector.xpath(topic_xpath_rule).extract() # for i in range(len(topic_ids)): print("?30???") # for i in range(10): for i in range(len(topic_ids)): params = {"topic_id": int(topic_ids[i]), "offset": 0, "hash_id": "d17ff3d503b2ebce086d2f3e98944d54"} yield FormRequest( url='https://www.zhihu.com/node/TopicsPlazzaListV2', method='POST', # headers=self.set_headers2('https://www.zhihu.com/topics'), headers=self.set_headers('https://www.zhihu.com/topics'), cookies=cookielib.LWPCookieJar(filename='cookies'), # formdata={'method': 'next', 'params': '{"topic_id":988,"offset":0,"hash_id":"d17ff3d503b2ebce086d2f3e98944d54"}'}, formdata={'method': 'next', 'params': str(params).replace("\'", "\"").replace(" ", "")}, callback=self.topic_parse, meta={'topic_name': topic_names[i]} )
def requestJSON(self, url, headers, body=None, method=None): addon_profile_path = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('profile')) cj = cookielib.LWPCookieJar(os.path.join(addon_profile_path, 'cookies.lwp')) try: cj.load(os.path.join(addon_profile_path, 'cookies.lwp'),ignore_discard=True) except: pass opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.addheaders = headers try: request = urllib2.Request(url, body) if method == 'DELETE': request.get_method = lambda: method response = opener.open(request) json_source = json.load(response) response.close() self.saveCookie(cj) except HTTPError as e: if e.code == 403: msg = 'Your device is not authorized to view the selected stream.\n Would you like to authorize this device now?' dialog = xbmcgui.Dialog() answer = dialog.yesno('Account Not Authorized', msg) if answer: self.registerDevice() sys.exit(0) return json_source
def getURL(url, host=BASE_URL.split('//')[1], useCookie=False, silent=False, headers=None): cj = cookielib.LWPCookieJar() if useCookie: if isinstance(useCookie, bool): cj = mechanizeLogin() else: cj = useCookie if isinstance(cj, bool): return False dispurl = re.sub('(?i)%s|%s|&token=\w+' % (tvdb, tmdb), '', url).strip() if not silent: Log('getURL: ' + dispurl) if not headers: headers = [('User-Agent', UserAgent), ('Host', host)] try: opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPRedirectHandler) opener.addheaders = headers usock = opener.open(url) response = usock.read() usock.close() except urllib2.URLError, e: Log('Error reason: %s' % e, xbmc.LOGERROR) return False return response
def __init__(self): self.url = None self.account = None self.default_headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0", "connection":"keep-alive", "Accept-Encoding": "gzip"} self._is_init = False self.load_cookies_success = False self.login_success = False self.logger = ZhihuLog.creatlogger(self.__class__.__name__) self.session = requests.Session() self.session.headers = self.default_headers self.session.cookies = cookielib.LWPCookieJar(gl.g_config_folder + 'cookiejar') if os.path.exists(gl.g_config_folder+'cookiejar'): self.session.cookies.load(ignore_discard=True) self.load_cookies_success = True
def loginStatus_s(self, response): soup = BeautifulSoup(response.text,"html.parser") if soup.find("div",{"class":"me"}) == None: print u"-----------????---------".decode(code).encode('gbk') #print 'content ', response.content login_uid = re.findall(r'href=\"/([0-9]+)/info', response.content,re.I) login_uid = login_uid[0] cookieInfo = requests.utils.dict_from_cookiejar(session.cookies) cj = cookielib.LWPCookieJar('loginInfo_'+login_uid+'.txt') requests.utils.cookiejar_from_dict(cookieInfo, cj) cj.save('cookies/loginInfo_'+login_uid+'.txt', ignore_discard=True, ignore_expires=True) with open('login_users.txt', 'a') as f: #?????uid, ?????? f.write(login_uid+'\n') f.close() else: print u"-----------???????????????????-----------" self.loginSimulation()
def loginStatus_s(self, response): soup = BeautifulSoup(response.text,"html.parser") if soup.find("div",{"class":"me"}) == None: print u"-----------????---------".decode(code).encode('gbk') print 'content ', response.content login_uid = re.findall(r'href=\"/([0-9]+)/info', response.content,re.I) login_uid = login_uid[0] print 'uid:',login_uid cookieInfo = requests.utils.dict_from_cookiejar(session.cookies) cj = cookielib.LWPCookieJar('loginInfo_'+login_uid+'.txt') requests.utils.cookiejar_from_dict(cookieInfo, cj) cj.save('cookies/loginInfo_'+login_uid+'.txt', ignore_discard=True, ignore_expires=True) with open('login_users.txt', 'a') as f: #?????uid, ?????? f.write(login_uid+'\n') f.close() else: print u"-----------???????????????????-----------" #self.loginSimulation()
def login(username, password): br = mechanize.Browser() login_url = "http://cyclebabac.com/wp-login.php" # Cookie Jar cj = cookielib.LWPCookieJar() br.set_cookiejar(cj) # Follows refresh 0 but not hangs on refresh > 0 br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) # Want debugging messages? Uncomment this #br.set_debug_http(True) #br.set_debug_redirects(True) #br.set_debug_responses(True) # Perform the actual login br.open(login_url) br.select_form(nr=0) br.form['log'] = str(username) br.form['pwd'] = str(password) br.submit() return br
def proxy_identify(proxy, url): cookie = cookielib.LWPCookieJar() handler = urllib2.HTTPCookieProcessor(cookie) proxy_support = urllib2.ProxyHandler({'http': proxy}) opener = urllib2.build_opener(proxy_support, handler) try: response = opener.open(url, timeout=3) if response.code == 200: c = '' for item in cookie: c += item.name+'='+item.value+';' print c IpProxy.sogou_cookie.append(c) return True except Exception, error: print error return False
def check_login(self): expired_cookies = True addon_profile_path = xbmc.translatePath(self.addon.getAddonInfo('profile')) try: cj = cookielib.LWPCookieJar() cj.load(os.path.join(addon_profile_path, 'cookies.lwp'), ignore_discard=True) if self.npsso != '': for cookie in cj: if cookie.name == 'npsso': expired_cookies = cookie.is_expired() break except: pass if expired_cookies: self.login()
def __init__(self): # ???? self.proxy_url = proxyList[3] self.proxy = urllib2.ProxyHandler({"http": self.proxy_url}) # ?? self.hostURL = 'http://book.douban.com/tag/' self.headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.47 (KHTML, like Gecko)' ' Chrome/48.1.2524.116 Safari/537.36', 'Referer': 'http://book.douban.com/', 'Host': 'book.douban.com', 'Upgrade-Insecure-Requests': '1', 'Connection': 'keep-alive' } # opener?? self.cookie = cookielib.LWPCookieJar() self.cookieHandler = urllib2.HTTPCookieProcessor(self.cookie) self.opener = urllib2.build_opener(self.cookieHandler, self.proxy, urllib2.HTTPHandler) # ????????????
def __init__(self, cookie_location="cookie.txt"): """ Base constructor. Responsible for setting up the requests object and cookie jar. All configuration values should be set prior to constructing an object of this type; changes to configuration will not take effect on variables which already exist. """ self.session = requests.Session() self.session.cookies = cookielib.LWPCookieJar(cookie_location) try: self.session.cookies.load() except IOError: # Cookie file doesn't exist. pass self.session.headers.update(self.default_headers) self.session.params.update(self.default_params)
def __init__(self, settings_folder, country, debug=False): self.debug = debug self.country = country self.settings_folder = settings_folder self.cookie_jar = cookielib.LWPCookieJar(os.path.join(self.settings_folder, 'cookie_file')) self.tempdir = os.path.join(settings_folder, 'tmp') if not os.path.exists(self.tempdir): os.makedirs(self.tempdir) self.deviceid_file = os.path.join(settings_folder, 'deviceId') self.http_session = requests.Session() self.device_key = 'xdk-%s' % self.country self.base_url = 'https://content.viaplay.{0}/{1}'.format(self.country, self.device_key) self.login_api = 'https://login.viaplay.%s/api' % self.country try: self.cookie_jar.load(ignore_discard=True, ignore_expires=True) except IOError: pass self.http_session.cookies = self.cookie_jar
def create_browser(): br = mechanize.Browser() # Create basic browser cj = cookielib.LWPCookieJar() # Create cookiejar to handle cookies br.set_cookiejar(cj) # Set cookie jar for our browser br.set_handle_equiv(True) # Allow opening of certain files br.set_handle_gzip(True) # Allow handling of zip files br.set_handle_redirect(True) # Automatically handle auto-redirects br.set_handle_referer(True) br.set_handle_robots(False) # ignore anti-robots.txt # Necessary headers to simulate an actual browser br.addheaders = [('User-agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.131 Safari/537.36'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'), ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3'), ('Accept-Encoding', 'gzip,deflate,sdch'), ('Accept-Language', 'en-US,en;q=0.8,fr;q=0.6'), ('Connection', 'keep-alive') ] return br
def clearCookie(self, cookiefile, leaveNames=[], removeNames=None, ignore_discard = True): try: toRemove = [] if not self.useMozillaCookieJar: cj = cookielib.LWPCookieJar() else: cj = cookielib.MozillaCookieJar() cj.load(cookiefile, ignore_discard = ignore_discard) for cookie in cj: if cookie.name not in leaveNames and (None == removeNames or cookie.name in removeNames): toRemove.append(cookie) for cookie in toRemove: cj.clear(cookie.domain, cookie.path, cookie.name) cj.save(cookiefile, ignore_discard = ignore_discard) except Exception: printExc() return False return True
def getInfo(ipaddr, userAgent, proxz, hostname): WEBFORM_NAME = 'search' browser = mechanize.Browser() browser.set_handle_robots(False) browser.set_handle_equiv(True) browser.set_handle_referer(True) browser.set_handle_redirect(True) browser.addheaders = userAgent # browser.set_proxies(proxz) cookie_jar = cookielib.LWPCookieJar() browser.set_cookiejar(cookie_jar) page = browser.open('https://apps.db.ripe.net/search/query.html') for form in browser.forms(): if form.name == WEBFORM_NAME: browser.select_form(WEBFORM_NAME) browser.form['search:queryString'] = ipaddr browser.form['search:sources'] = ['GRS'] submission = browser.submit().read() parsed_submission = BeautifulSoup(submission, 'html.parser') print ipaddr, '/',hostname for mainIndex in parsed_submission.find_all('ul', {'class': 'attrblock'}): for i, li in enumerate(mainIndex.findAll('li')): if i in range(0, 2): print '[+] ', li.text print '\n ########## \n'
def __init__(self): self.br = mechanize.Browser() #self.cj = cookielib.LWPCookieJar() self.cj = cookielib.MozillaCookieJar() self.br.set_cookiejar(self.cj) self.br.set_handle_equiv(True) self.br.set_handle_referer(True) self.br.set_handle_robots(False) self.br.addheaders = [('User-agent', 'Firefox')] self.item_url = 'http://shop.bdgastore.com/collections/footwear/products/y-3-pureboost-zg' # Create variables for user credentials and a function to import them
def send_cookie_auth(self, connection): """Include Cookie Authentication data in a header""" cj = cookielib.LWPCookieJar() cj.load(self.cookiefile) for cookie in cj: if cookie.name == 'UUID': uuidstr = cookie.value connection.putheader(cookie.name, cookie.value) ## override the send_host hook to also send authentication info
def get_file(self, url, quality): self.cookieJar = cookielib.LWPCookieJar() self.opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(self.cookieJar), urllib2.HTTPRedirectHandler(), urllib2.HTTPHandler(debuglevel=0)) self.opener.addheaders = [('User-agent', "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36")] forms = {"youtubeURL": url, 'quality':quality } data = urllib.urlencode(forms) req = urllib2.Request('http://www.convertmemp3.com/',data) res = self.opener.open(req) self.convhtml = res.read()
def __urllib2Opener(): """ This function creates the urllib2 OpenerDirector. """ global authHandler global proxyHandler debugMsg = "creating HTTP requests opener object" logger.debug(debugMsg) conf.cj = cookielib.LWPCookieJar() opener = urllib2.build_opener(proxyHandler, authHandler, urllib2.HTTPCookieProcessor(conf.cj)) urllib2.install_opener(opener)
def __init__(self, proxyHandler): self.__googleCookie = None self.__matches = [] self.__cj = cookielib.LWPCookieJar() self.opener = urllib2.build_opener(proxyHandler, urllib2.HTTPCookieProcessor(self.__cj)) self.opener.addheaders = conf.httpHeaders
def login(self, username, pwd, cookie_file): """" Login with use name, password and cookies. (1) If cookie file exists then try to load cookies; (2) If no cookies found then do login """ # If cookie file exists then try to load cookies if os.path.exists(cookie_file): try: cookie_jar = cookielib.LWPCookieJar(cookie_file) cookie_jar.load(ignore_discard=True, ignore_expires=True) loaded = 1 except cookielib.LoadError: loaded = 0 LOG.info('Loading cookies error') # install loaded cookies for urllib2 if loaded: cookie_support = urllib2.HTTPCookieProcessor(cookie_jar) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) LOG.info('Loading cookies success') return 1 else: return self.do_login(username, pwd, cookie_file) else: # If no cookies found return self.do_login(username, pwd, cookie_file)
def save_cookie(self, text, cookie_file=CONF.cookie_file): cookie_jar2 = cookielib.LWPCookieJar() cookie_support2 = urllib2.HTTPCookieProcessor(cookie_jar2) opener2 = urllib2.build_opener(cookie_support2, urllib2.HTTPHandler) urllib2.install_opener(opener2) if six.PY3: text = text.decode('gbk') p = re.compile('location\.replace\(\'(.*?)\'\)') # ???httpfox?????????????? # location.replace('http://weibo.com ????????? # ?????????????# ????login_url?? ??????re????? # p = re.compile('location\.replace\(\B'(.*?)'\B\)') # ??? ??????? re?????\'??????? try: # Search login redirection URL login_url = p.search(text).group(1) data = urllib2.urlopen(login_url).read() # Verify login feedback, check whether result is TRUE patt_feedback = 'feedBackUrlCallBack\((.*)\)' p = re.compile(patt_feedback, re.MULTILINE) feedback = p.search(data).group(1) feedback_json = json.loads(feedback) if feedback_json['result']: cookie_jar2.save(cookie_file, ignore_discard=True, ignore_expires=True) return 1 else: return 0 except: return 0
def login(self, username, pwd, cookie_file): """" Login with use name, password and cookies. (1) If cookie file exists then try to load cookies; (2) If no cookies found then do login """ #If cookie file exists then try to load cookies if os.path.exists(cookie_file): try: cookie_jar = cookielib.LWPCookieJar(cookie_file) cookie_jar.load(ignore_discard=True, ignore_expires=True) loaded = 1 except cookielib.LoadError: loaded = 0 print 'Loading cookies error' #install loaded cookies for urllib2 if loaded: cookie_support = urllib2.HTTPCookieProcessor(cookie_jar) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) print 'Loading cookies success' return 1 else: return self.do_login(username, pwd, cookie_file) else: #If no cookies found return self.do_login(username, pwd, cookie_file)
def login(self, username, pwd, cookie_file): """" Login with use name, password and cookies. (1) If cookie file exists then try to load cookies; (2) If no cookies found then do login """ # If cookie file exists then try to load cookies if os.path.exists(cookie_file): try: cookie_jar = cookielib.LWPCookieJar(cookie_file) cookie_jar.load(ignore_discard=True, ignore_expires=True) loaded = 1 except cookielib.LoadError: loaded = 0 print('Loading cookies error') #install loaded cookies for urllib2 if loaded: cookie_support = urllib2.HTTPCookieProcessor(cookie_jar) opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) print('Loading cookies success') return 1 else: return self.do_login(username, pwd, cookie_file) else: #If no cookies found return self.do_login(username, pwd, cookie_file)
def resolve_value_of_cookies(element): """ This function evaluates user input for cookies. If a file path is given, then a cookiejar object is created and the contents of the file are loaded into the object. This object is then returned. Else, a dictionary would be created out of the string given input = "foo=foo1; bar=bar1; ; =foobar; barfoo=" return value = {'foo': 'foo1', 'bar': 'bar1'} If the dictionary is empty at the end of the function, None is retuened. """ if element is not None and element is not False and element != "": abs_path = file_Utils.getAbsPath(element, sys.path[0]) if os.path.exists(abs_path): element = cookielib.LWPCookieJar(element) try: element.load() except cookielib.LoadError: pNote("Cookies could not be loaded from {}.".format(element), "error") element = None except Exception as e: pNote("An Error Occurred: {0}".format(e), "error") else: element = convert_string_to_dict(element) else: element = None return element
def EnableCookie(self, enableProxy): #"Enable cookie & proxy (if needed)." cookiejar = cookielib.LWPCookieJar()#construct cookie cookie_support = urllib2.HTTPCookieProcessor(cookiejar) if enableProxy: proxy_support = urllib2.ProxyHandler({'http':'http://xxxxx.pac'})#use proxy opener = urllib2.build_opener(proxy_support, cookie_support, urllib2.HTTPHandler) print ("Proxy enabled") else: opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener)#construct cookie's opener
def __init__(self): self.header = { 'Accept': '*/*', 'Accept-Encoding': 'gzip,deflate,sdch', 'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4', 'Connection': 'keep-alive', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'music.163.com', 'Referer': 'http://music.163.com/search/', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.152 Safari/537.36' # NOQA } self.cookies = {'appver': '1.5.2'} self.playlist_class_dict = {} self.session = requests.Session() self.storage = Storage() self.session.cookies = LWPCookieJar(self.storage.cookie_path) try: self.session.cookies.load() self.file = file(self.storage.cookie_path, 'r') cookie = self.file.read() self.file.close() pattern = re.compile(r'\d{4}-\d{2}-\d{2}') str = pattern.findall(cookie) if str: if str[0] < time.strftime('%Y-%m-%d', time.localtime(time.time())): self.storage.database['user'] = { 'username': '', 'password': '', 'user_id': '', 'nickname': '', } self.storage.save() os.remove(self.storage.cookie_path) except IOError as e: log.error(e) self.session.cookies.save()
def __init__(self): """ Create a UnifiClient object. """ self.version = settings.UNIFI_VERSION self.site_id = settings.UNIFI_SITE_ID self.__unifiUser = settings.UNIFI_USER self.__unifiPass = settings.UNIFI_PASSWORD self.__unifiServer = settings.UNIFI_SERVER self.__unifiPort = settings.UNIFI_PORT self.__cookie_file = "/tmp/unifi_cookie" # Use a Session object to handle cookies. self.__session = requests.Session() cj = cookielib.LWPCookieJar(self.__cookie_file) # Load existing cookies (file might not yet exist) try: cj.load() except: pass self.__session.cookies = cj # Use an SSLAdapter to work around SSL handshake issues. self.__session.mount(self._get_resource_url(), SSLAdapter(ssl.PROTOCOL_SSLv23)) pass
def __init__(self, cookie_filename=None, timeout=None, **kwargs): self.cj = cookielib.LWPCookieJar() if cookie_filename is not None: self.cj.load(cookie_filename) self.cookie_processor = urllib2.HTTPCookieProcessor(self.cj) self.__build_opener() urllib2.install_opener(self.opener) if timeout is None: # self._default_timeout = socket._GLOBAL_DEFAULT_TIMEOUT # Set default timeout self._default_timeout = 5 else: self._default_timeout = timeout
def __init__(self, cookie_filename=None, user_agent=None, timeout=None, **kwargs): try: import mechanize except ImportError: raise DependencyNotInstalledError('mechanize') if user_agent is None: user_agent = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)' self.browser = mechanize.Browser() self.cj = cookielib.LWPCookieJar() if cookie_filename is not None: self.cj.load(cookie_filename) self.browser.set_cookiejar(self.cj) self.browser.set_handle_equiv(True) self.browser.set_handle_gzip(True) self.browser.set_handle_redirect(True) self.browser.set_handle_referer(True) self.browser.set_handle_robots(False) self.browser.addheaders = [ ('User-agnet', user_agent) ] if timeout is None: # self._default_timout = mechanize._sockettimeout._GLOBAL_DEFAULT_TIMEOUT self._default_timout = 5 else: self._default_timout = timeout
def init(self, proxy=None): cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) if proxy: proxy_support = urllib2.ProxyHandler({'http': proxy}) opener = urllib2.build_opener(proxy_support, cookie_support, urllib2.HTTPHandler) else: opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener) #print 'seton'
def use_proxy(self, proxy): """ ????????,??proxy?????????????:http://XX.XX.XX.XX:XXXX """ cj = cookielib.LWPCookieJar() cookie_support = urllib2.HTTPCookieProcessor(cj) if proxy: proxy_support = urllib2.ProxyHandler({'http': proxy}) opener = urllib2.build_opener(proxy_support, cookie_support, urllib2.HTTPHandler) else: opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) urllib2.install_opener(opener)
def __init__(self,url): cookie_jar = cookielib.LWPCookieJar() cookie = urllib2.HTTPCookieProcessor(cookie_jar) self.opener = urllib2.build_opener(cookie) user_agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36" self.url=url self.send_headers={'User-Agent':user_agent}
def test_lwp_valueless_cookie(self): # cookies with no value should be saved and loaded consistently from cookielib import LWPCookieJar filename = test_support.TESTFN c = LWPCookieJar() interact_netscape(c, "http://www.acme.com/", 'boo') self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None) try: c.save(filename, ignore_discard=True) c = LWPCookieJar() c.load(filename, ignore_discard=True) finally: try: os.unlink(filename) except OSError: pass self.assertEqual(c._cookies["www.acme.com"]["/"]["boo"].value, None)
def get_verifycode(): global cookie cookie = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie), urllib2.HTTPHandler) urllib2.install_opener(opener) img = urllib2.urlopen('http://xk.urp.seu.edu.cn/jw_css/getCheckCode', timeout=8) f = open('verifycode.jpg', 'wb') f.write(img.read()) f.close() return 0
def __init__(self, headers = {},debug = True, p = ''): #timeout self.timeout = 10 #cookie handler self.cookie_processor = urllib2.HTTPCookieProcessor(cookielib.LWPCookieJar()) #debug handler self.debug = debug if self.debug: self.httpHandler = urllib2.HTTPHandler(debuglevel=1) self.httpsHandler = urllib2.HTTPSHandler(debuglevel=1) else: self.httpHandler = urllib2.HTTPHandler(debuglevel=0) self.httpsHandler = urllib2.HTTPSHandler(debuglevel=0) #proxy handler (http) if p != '' and p != 'None' and p != None and p != 'NULL': self.proxy_handler = urllib2.ProxyHandler({'http': p}) else: self.proxy_handler = urllib2.ProxyHandler({}) #opener self.opener = urllib2.build_opener( self.cookie_processor,self.proxy_handler, self.httpHandler, self.httpsHandler) self.opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'),] #header for key in headers.keys(): cur=self._replace(key) if cur!=-1: self.opener.addheaders.pop(cur) self.opener.addheaders += [(key, headers[key]), ]
def __init__(self): import cookielib self._cookies = None self.cookies = cookielib.LWPCookieJar() self.content = None self.status = None
def install_cookiehandler(): url = 'http://www.ticaihui.com/' c = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(c)) urllib2.install_opener(opener) req = urllib2.Request(url) resp = urllib2.urlopen(req) #???????