def test_login_or_logout(self, client, service, user_data): from cookielib import Cookie args = [ None, None, '', None, None, '/', None, None, 86400, None, None, None, None ] gaema_user_key = GAEMA_USER_KEY_FORMAT % service if hasattr(settings, "GAEMA_STORAGE") and \ settings.GAEMA_STORAGE == "cookie": client.cookie_jar.set_cookie( Cookie(1, gaema_user_key, user_data, *args)) else: session_store = import_string(settings.SESSION_STORE)() data = None for cookie in client.cookie_jar: if cookie.name == settings.COOKIE_NAME: data = cookie.value if data is None: session = session_store.new() else: session = session_store.get(data) session[gaema_user_key] = user_data session_store.save(session) data = "\"%s\"" % session_store.get_data(session) client.cookie_jar.set_cookie( Cookie(1, settings.COOKIE_NAME, data, *args))
def _really_load(self): """ Implementation of the _really_load method. Basically, it just loads everything in the database, and maps it to cookies """ try: with sqlite3.connect(self.filename, self.timeout) as con: con.row_factory = sqlite3.Row res = con.execute("SELECT * from cookie").fetchall() for cookie in res: initial_dot = cookie["domain"].startswith(".") c = Cookie( 0, cookie["name"], cookie["value"], None, False, cookie["domain"], initial_dot, initial_dot, cookie["path"], cookie["path"]!="/", cookie["secure"], cookie["expiry"], False, None, None, {} ) self.logger.info( "LOADED cookie [domain: %s , name : %s, value: %s]" % (c.domain, c.name, c.value) ) if not c.is_expired(time.time()): self.set_cookie(c) except sqlite3.DatabaseError, e: self.logger.error("Loading cookies failed : could not access database") self.logger.error("Exception was : %s - %s" % (type(e).__name__, e))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "%r does not look like a Netscape format cookies file" % filename) try: while True: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Netscape format cookies file %r: %r" % (filename, line))
def load(self, loadStr, ignore_discard=True , ignore_expires=True): now = time.time() lines=loadStr.split('\n') for line in lines: if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c)
def set_session_cookie(self, username, password): """ Sets a session with Django test client and logs Ghost in by creating a session cookie for it. Args: username (str): The username to login with. password (str): The password to login with. """ client = Client(enforce_csrf_checks=False) self.assertEqual(client.login(username=username, password=password), True) sessionid = client.cookies['sessionid'] django_cookie = Cookie(version=0, name='sessionid', value=sessionid.value, port=None, port_specified=False, domain='localhost', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest=None, rfc2109=False) cj = CookieJar() cj.set_cookie(django_cookie) self.ghost.load_cookies(cj)
def HarVest(Phone, Amount): for _ in range(Amount): sleep(5) headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9', 'Referer': 'https://harvestcakes.com/register' } data = {'phone': Phone, 'url': ''} Sess = requests.Session() Sess.cookies = Cookie('.cookieslog') Sess.headers = headers postData = Sess.post('https://harvestcakes.com/register', data=data, allow_redirects=True) if 'Enter OTP code we sent via SMS on phone number' in postData.text: print(W + '[' + C + '*' + W + '] KIRIM SPAM KE NOMOR ' + C + str(Phone) + W + ' BERHASIL' + H + ' \xE2\x9C\x94') Sess.cookies.save() else: print(W + '[' + C + '*' + W + '] KIRIM SPAM KE NOMOR ' + C + str(Phone) + W + ' GAGAL' + M + ' \xE2\x9C\x96')
def toPyCookie(QtCookie): port = None port_specified = False secure = QtCookie.isSecure() name = str(QtCookie.name()) value = str(QtCookie.value()) v = str(QtCookie.path()) path_specified = bool(v != "") path = v if path_specified else None v = str(QtCookie.domain()) domain_specified = bool(v != "") domain = v if domain_specified: domain_initial_dot = v.startswith('.') else: domain_initial_dot = None v = long(QtCookie.expirationDate().toTime_t()) # Long type boundary on 32bit platfroms; avoid ValueError expires = 2147483647 if v > 2147483647 else v rest = {} discard = False return Cookie(0, name, value, port, port_specified, domain, domain_specified, domain_initial_dot, path, path_specified, secure, expires, discard, None, None, rest)
def add_cookies(self, cookie_handler, cookies, url=None): """ Add cookies to the specified cookie jar. Domain for the cookie can be specified via url. """ domain = urlsplit(url or self.login_url) domain = ".%s.%s" % (domain.netloc.split(".")[-2], domain.netloc.split(".")[-1]) for _ in parse_qsl(cookies, 1): cookie = Cookie(version=0, name=_[0], value=_[1], port=None, port_specified=False, domain=domain, domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=True, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False) cookie_handler.set_cookie(cookie)
def Olx(Phone, Amount): for _ in range(Amount): sleep(5) headers = { 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9', 'referer': 'https://www.olx.co.id/' } data = {'grantType': 'phone', 'phone': Phone, 'language': ''} Sess = requests.Session() Sess.cookies = Cookie('.cookieslog') Sess.headers = headers postData = Sess.post('https://www.olx.co.id/api/auth/authenticate', json=data, allow_redirects=True) if 'PIN' and 'token' in postData.text: print(W + '[' + C + '*' + W + '] KIRIM SPAM KE NOMOR ' + C + str(Phone) + W + ' BERHASIL' + H + ' \xE2\x9C\x94') Sess.cookies.save() else: print(W + '[' + C + '*' + W + '] KIRIM SPAM KE NOMOR ' + C + str(Phone) + W + ' GAGAL' + M + ' \xE2\x9C\x96')
def Hooq(Phone, Amount): for _ in range(Amount): sleep(5) bro = mechanize.Browser() Cookies = Cookie('.cookieslog') bro.set_handle_robots(False) bro.set_cookiejar(Cookies) bro.addheaders = [ ('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9'), ('Referer', 'https://authenticate.hooq.tv/signupmobile?return?Url=https://www.hooq.tv/auth//verify/ev%2F%257Cid') ] bro.open('https://authenticate.hooq.tv/signupmobile?returnUrl=https://www.hooq.tv%2Fauth%2Fverify%2Fev%2F%257Cid&serialNo=0&deviceType=webClient&modelNo=webclient-aurora&deviceName=webclient-aurora/production-8.4.1&deviceSignature=0') bro.select_form(nr=0) bro.form ['mobile'] = str(Phone) submit = bro.submit() if 'Your+number+has+been+blocked.' in submit.geturl(): print(W+'NOMOR TARGET UDAH DI BLOKIR\n'+C+'SAMA PIHAK HOOQ. '+W+'COBA NOMOR LAIN ^_^') sys.exit() elif 'buCode' in submit.geturl(): print(W+'['+C+'*'+W+'] KIRIM SPAM KE NOMOR '+C+str(Phone)+W+' BERHASIL'+H+' \xE2\x9C\x94') else: print(W+'['+C+'*'+W+'] KIRIM SPAM KE NOMOR '+C+str(Phone)+W+' GAGAL'+M+' \xE2\x9C\x96')
def start(args): """Login and session handler """ # create cookiejar args._cj = LWPCookieJar() # lets urllib handle cookies opener = build_opener(HTTPCookieProcessor(args._cj)) opener.addheaders = [( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36" ), ("Accept-Encoding", "identity"), ("Accept-Charset", "utf-8"), ("DNT", "1")] install_opener(opener) # load cookies try: args._cj.load(getCookiePath(args), ignore_discard=True) except IOError: # cookie file does not exist pass args._cj.set_cookie( Cookie(0, "timezoneoffset", str(timezone // 60), None, False, "www.wakanim.tv", False, False, "/", True, False, None, False, None, None, {"HttpOnly": None}, False))
def AlfaMart(Phone, Amount): for _ in range(Amount): sleep(5) headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko', 'Referer' : 'https://www.alfacart.com/login?tab=daftar&return=' } data = { 'fullName' : 'IG sad.boyz01', 'red' : 'customer%2Faccount', 'email' : '*****@*****.**', 'password' : 'Follow IG Gua Yak', 'isNewsletter' : 'on' } Sess = requests.Session() Sess.cookies = Cookie('.cookieslog') Sess.headers = headers postData = Sess.post('https://www.alfacart.com/getMemberInfo', data = {'email' : '*****@*****.**'}) postData_2 = Sess.post('https://www.alfacart.com/otp/checkActiveNumber', data = {'phoneNumber' : Phone, 'email' : '*****@*****.**'}) postData_3 = Sess.post('https://www.alfacart.com/otp/phoneNumberRegistration', data = {'phoneNumber' : Phone}) postData_4 = Sess.post('https://www.alfacart.com/doRegister', data = data) if 'responseMessage":"Nomor terverifikasi dan dapat diubah.","status":true' in postData_2.text: print(W+'['+C+'*'+W+'] KIRIM SPAM KE NOMOR '+C+str(Phone)+W+' BERHASIL'+H+' \xE2\x9C\x94') Sess.cookies.save() else: print(W+'['+C+'*'+W+'] KIRIM SPAM KE NOMOR '+C+str(Phone)+W+' GAGAL'+M+' \xE2\x9C\x96')
def Phd(Phone, Amount): if str(Phone).startswith('08', 0) == True: Phone = Phone.replace('08', '8') for _ in range(Amount): sleep(5) headers = { 'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9', 'Referer' : 'https://www.phd.co.id/en/users/createnewuser' } data = { 'requests_id' : '', 'first_name' : 'Follow IG', 'last_name' : 'sad.boyz01', 'gender' : 'male', 'phone_number' : Phone, 'birthday' : '0000-00-00', 'username' : '*****@*****.**', 'password' : 'Follow 1000x Ya Anying', 'agreeterms' : '1' } Sess = requests.Session() Sess.cookies = Cookie('.cookieslog') Sess.headers = headers postData = Sess.post('http://www.phd.co.id/en/users/createNewUser', data = data , allow_redirects = True) Sess.cookies.save() print(W+'['+C+'*'+W+'] KIRIM SPAM KE NOMOR '+C+str(Phone)+W+' BERHASIL'+H+' \xE2\x9C\x94')
def set_cookie(self, name, value, domain=None, path=None, session=True, expires=None, port=None, request=None): """ :param expires: Seconds from epoch :param port: must match request port :param domain: the fqn of your server hostname """ # Cookie(version, name, value, port, port_specified, # domain, domain_specified, domain_initial_dot, # path, path_specified, secure, expires, # discard, comment, comment_url, rest, # rfc2109=False): cookie = Cookie(0, name, value, port, bool(port), domain or '', bool(domain), (domain and domain.startswith('.')), path or '', bool(path), False, expires, session, None, None, {}, False) self._cookie_jar.set_cookie(cookie)
def phdx(no): if str(no).startswith('08', 0) == True: no = no.replace('08', '8') headers = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0.1; SM-G532G) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Mobile Safari/537.36', 'Referer': 'https://www.phd.co.id/en/users/createnewuser' } r = requests.Session() r.cookies = Cookie('.cookieslog') r.headers = headers data = { 'requests_id': '', 'first_name': 'Subscribe YT', 'last_name': 'Jejak Cyber', 'gender': 'male', 'phone_number': no, 'birthday': '0000-00-00', 'username': '******', 'password': '******', 'agreeterms': '1' } send = r.post( 'http://www.phd.co.id/en/users/createNewUser', data=data, allow_redirects=True) r.cookies.save() if 'kontol' in send.text: print p + '{' + h + '\xe2\x9c\x93' + p + '} ' + h + 'Spam Sms ' + k + '0' + str( no) + p + ' { ' + h + 'Success ' + p + '}'
def py_cookies(self): for c in self.cookie_jar.allCookies(): name, value = map(bytes, (c.name(), c.value())) domain = bytes(c.domain()) initial_dot = domain_specified = domain.startswith(b'.') secure = bool(c.isSecure()) path = unicode(c.path()).strip().encode('utf-8') expires = c.expirationDate() is_session_cookie = False if expires.isValid(): expires = expires.toTime_t() else: expires = None is_session_cookie = True path_specified = True if not path: path = b'/' path_specified = False c = Cookie(0, # version name, value, None, # port False, # port specified domain, domain_specified, initial_dot, path, path_specified, secure, expires, is_session_cookie, None, # Comment None, # Comment URL {} # rest ) yield c
def download_gazette(self, relpath, search_url, postdata, metainfo, cookiejar): relurl = self.get_relurl(relpath, metainfo) if not relurl: self.logger.warn('Not able to form relurl for %s', metainfo) return None newpost = [] for d in postdata: if d[0] == 'ctl00$ContentPlaceHolder2$btnShow': continue newpost.append(d) gzid = self.post_for_gzid(newpost) if not gzid: self.logger.warn('Unable to get gazette id in the cookie for %s', relurl) return None fileurl = self.file_url % gzid metainfo.pop('download') cookie = Cookie(0, self.filenum_cookie, gzid, None, False, \ self.hostname, True, False, '/', True, False, \ None, False, None, None, None) cookiejar.set_cookie(cookie) if self.save_gazette(relurl, fileurl, metainfo, cookiefile=cookiejar,\ validurl = False): return relurl return None
def create_cookie(name, value, **kwargs): """Creates `cookielib.Cookie` instance. """ config = dict( name=name, value=value, version=0, port=None, domain='', path='/', secure=False, expires=None, discard=True, comment=None, comment_url=None, rfc2109=False, rest={'HttpOnly': None}, # wtf? ) bad_args = set(kwargs) - set(config.keys()) if bad_args: raise TypeError('Unexpected arguments: %s' % tuple(bad_args)) config.update(**kwargs) config['port_specified'] = bool(config['port']) config['domain_specified'] = bool(config['domain']) config['domain_initial_dot'] = config['domain'].startswith('.') config['path_specified'] = bool(config['path']) return Cookie(**config)
def _read_cookies(self, url=''): cookies_path = os.path.join(PATH_TEMP, 'burst') if not os.path.exists(cookies_path): try: os.makedirs(cookies_path) except Exception as e: log.debug("Error creating cookies directory: %s" % repr(e)) self._cookies_filename = os.path.join(cookies_path, urlparse(url).netloc + '_cookies.jar') if os.path.exists(self._cookies_filename): try: self._cookies.load(self._cookies_filename) except Exception as e: log.debug("Reading cookies error: %s" % repr(e)) # Check for cf_clearance cookie # https://github.com/scakemyer/cloudhole-api if self.clearance and not any(cookie.name == 'cf_clearance' for cookie in self._cookies): c = Cookie(version=None, name='cf_clearance', value=self.clearance[13:], port=None, port_specified=False, domain='.{uri.netloc}'.format(uri=urlparse(url)), domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest=None, rfc2109=False) self._cookies.set_cookie(c)
def init(cookieFile=None): if cookieFile is None: if 'COOKIE_FILE' not in os.environ: warning("cs.www.init(): no \$COOKIE_FILE") return cookieFile = os.environ['COOKIE_FILE'] cookieHandler = HTTPCookieProcessor() cookieHandler.cookiejar = MozillaCookieJar() if not cookieFile.endswith('.sqlite'): # presume older mozilla cookie file cookieHandler.cookiejar.load(cookieFile) else: import sqlite3 import time now = time.time() debug("SQLITE3 cookie file: %s" % cookieFile) db = sqlite3.connect(cookieFile) cursor = db.cursor() cursor.execute( 'select id, name, value, host, path, expiry, isSecure, isHttpOnly from moz_cookies' ) for id, name, value, host, path, expiry, isSecure, isHttpOnly in cursor: isSecure = bool(isSecure) isHttpOnly = bool(isHttpOnly) if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = host.startswith(".") domain_specified = initial_dot discard = False if expiry == "": expiry = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, host, domain_specified, initial_dot, path, False, isSecure, expiry, discard, None, None, {}) if c.is_expired(now): warning("skip expired cookie: name=%s, host=%s, path=%s", name, host, path) continue warning("add cookie: name=%s, host=%s, path=%s", name, host, path) cookieHandler.cookiejar.set_cookie(c) install_opener(build_opener(cookieHandler))
def _authenticate(self, username, password): ''' Authenticate to Google: 1 - make a GET request to the Login webpage so we can get the login form 2 - make a POST request with email, password and login form input values ''' # Make sure we get CSV results in English ck = Cookie(version=0, name='I4SUserLocale', value='en_US', port=None, port_specified=False, domain='www.google.com', domain_specified=False,domain_initial_dot=False, path='/trends', path_specified=True, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest=None) ck_pref = Cookie(version=0, name='PREF', value='', port=None, port_specified=False, domain='www.google.com', domain_specified=False,domain_initial_dot=False, path='/trends', path_specified=True, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest=None) self.cj = CookieJar() self.cj.set_cookie(ck) self.cj.set_cookie(ck_pref) self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) self.opener.addheaders = self.headers # Get all of the login form input values find_inputs = etree.XPath("//form[@id='gaia_loginform']//input") try: # resp = self.opener.open(self.url_login) if resp.info().get('Content-Encoding') == 'gzip': buf = StringIO( resp.read()) f = gzip.GzipFile(fileobj=buf) data = f.read() else: data = resp.read() xmlTree = etree.fromstring(data, parser=html.HTMLParser(recover=True, remove_comments=True)) for input in find_inputs(xmlTree): name = input.get('name') if name: name = name.encode('utf8') value = input.get('value', '').encode('utf8') self.login_params[name] = value except: print("Exception while parsing: %s\n" % traceback.format_exc()) self.login_params["Email"] = username self.login_params["Passwd"] = password params = urllib.urlencode(self.login_params) self.opener.open(self.url_authenticate, params)
def _authenticate(self, username, password): ''' Authenticate to Google: 1 - make a GET request to the Login webpage so we can get the login form 2 - make a POST request with email, password and login form input values ''' # Make sure we get CSV results in English ck1 = Cookie(version=0, name='I4SUserLocale', value='en_US', port=None, port_specified=False, domain='.google.com', domain_specified=False,domain_initial_dot=False, path='', path_specified=False, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest=None) # This cookie is now mandatory ck2 = Cookie(version=0, name='PREF', value='', port=None, port_specified=False, domain='.google.com', domain_specified=False,domain_initial_dot=False, path='', path_specified=False, secure=False, expires=None, discard=False, comment=None, comment_url=None, rest=None) self.cj = CookieJar() self.cj.set_cookie(ck1) self.cj.set_cookie(ck2) self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) self.opener.addheaders = self.headers # Get all of the login form input values find_inputs = etree.XPath("//form[@id='gaia_loginform']//input") resp = self.opener.open(self.url_login) data = self.read_gzipped_response(resp) try: xmlTree = etree.fromstring(data, parser=html.HTMLParser(recover=True, remove_comments=True)) for input in find_inputs(xmlTree): name = input.get('name') if name: name = name.encode('utf8') value = input.get('value', '').encode('utf8') self.login_params[name] = value except: print("Exception while parsing: %s\n" % traceback.format_exc()) self.login_params["Email"] = username self.login_params["Passwd"] = password params = urllib.urlencode(self.login_params) auth_resp = self.opener.open(self.url_authenticate, params) # Testing whether Authentication was a success # I noticed that a correct auth sets a few cookies if not self.is_authentication_successfull(auth_resp): print 'Warning: Authentication failed for user %s' % username else: print 'Authentication successfull for user %s' % username
def load(self, loadStr, ignore_discard=True, ignore_expires=True): now = time.time() lines = loadStr.split('\n') for line in lines: if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c)
def load_cookie(self, c, ignore_discard=False, ignore_expires=False): expires = c['expires'] discard = c['discard'] if expires is not None else True domain = c['domain'] domain_specified = domain.startswith(".") c = Cookie(c['version'], c['name'], c['value'], c['port'], c['port_specified'], domain, domain_specified, c['domain_initial_dot'], c['path'], c['path_specified'], c['secure'], expires, discard, c['comment'], c['comment_url'], {}) if not ignore_discard and c.discard: return if not ignore_expires and c.is_expired(): return self.set_cookie(c)
def test_request_loader_does_not_fail_with_invalid_token(client): c = Cookie(version=0, name='remember_token', value='None', port=None, port_specified=False, domain='www.example.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) client.cookie_jar.set_cookie(c) response = client.get('/') assert b'BadSignature' not in response.data
def __init__(self, cookie_string): http_only_check = cookie_string.partition('#HttpOnly_') http_only = bool(http_only_check[1]) if http_only: cookie_string = http_only_check[-1] domain, domain_initial_dot, path, secure, expiration, name, value = cookie_string.split('\t') domain_initial_dot = domain_initial_dot.lower() == 'true' secure = secure.lower() == 'true' rest = {'httponly': None} if http_only else {} expiration = int(expiration) or None Cookie.__init__(self, version=0, name=name, value=value, port=None, port_specified=False, domain=domain, domain_specified=bool(domain), domain_initial_dot=domain_initial_dot, path=path, path_specified=bool(path), secure=secure, expires=expiration, discard=not expiration, comment=None, comment_url=None, rest=rest, rfc2109=False)
def test_token_loader_does_not_fail_with_invalid_token(self): c = Cookie(version=0, name='remember_token', value='None', port=None, port_specified=False, domain='www.example.com', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False) self.client.cookie_jar.set_cookie(c) r = self._get('/') self.assertNotIn(b'BadSignature', r.data)
def set_cookie(version=0, name='', value='', port=None, # Not used: port_specified=False, domain='', # Not used: domain_specified=True, domain_initial_dot=False, path='/', # Not used: path_specified=True, secure=False, expires=4102444555, # Not used: # discard=False, # comment=None, # comment_url=None, # rest=None, # rfc2109=False ): """ Sets a cookie in the UriHandler cookie jar :param int version: The cookie version :param str name: The name of the cookie :param str value: The value of the cookie :param int|None port: String representing a port or a set of ports (eg. '80', or '80,8080'), or None :param str domain: The domain for which the cookie should be valid :param bool domain_initial_dot: If the domain explicitly specified by the server began with a dot ('.'). :param str path: The path the cookie is valid for :param bool secure: If cookie should only be returned over a secure connection :param int expires: Integer expiry date in seconds since epoch, or None. :return: The new cookie. :rtype: cookielib.Cookie """ Logger.debug("Setting a cookie with this data:\n" "name: '%s'\n" "value: '%s'\n" "domain: '%s'\n" "path: '%s'", name, value, domain, path) c = Cookie(version=version, name=name, value=value, port=port, port_specified=port is not None, domain=domain, domain_specified=domain is not None, domain_initial_dot=domain_initial_dot, path=path, path_specified=path is not None, secure=secure, expires=expires, discard=False, comment=None, comment_url=None, rest={'HttpOnly': None}) # rfc2109=False) # the rfc2109 parameters is not valid in Python 2.4 (Xbox), so we ommit it. UriHandler.instance().cookieJar.set_cookie(c) return c
def auth_cookies(login): environ = dict(HTTP_HOST='example.com') cookies = [c for _, c in remember(DummyRequest(environ=environ), login)] for data in parse_ns_headers(cookies): name, value = data.pop(0) rest = dict(port='80', port_specified=True, domain_specified=True, path_specified=True, secure=False, expires=None, discard=False, comment='', comment_url=None, rest=None, domain='') rest.update(dict(data)) rest['domain_initial_dot'] = rest['domain'].startswith('.') yield Cookie(name=name, value=value, **rest)
def load(self, key, ignore_discard=False, ignore_expires=False): now = time.time() line = '' try: rvalue = self.redis.get(key) if rvalue is None: return lines = rvalue.split("\n") for line in lines: if line == '': break if line.endswith("\n"): line = line[:-1] #注释 if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == '': name = value value = None # initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if ignore_discard and c.discard: continue if ignore_expires and c.is_expired(now): continue self.set_cookie(c) except Exception: #_warn_unhandled_exception() raise LoadError("invalid format cookies redis %s: %r" % (key, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError('%r does not look like a Netscape format cookies file' % filename) try: while 1: line = f.readline() if line == '': break if line.endswith('\n'): line = line[:-1] if line.strip().startswith(('#', '$')) or line.strip() == '': continue domain, domain_specified, path, secure, expires, name, value = line.split('\t') secure = secure == 'TRUE' domain_specified = domain_specified == 'TRUE' if name == '': name = value value = None initial_dot = domain.startswith('.') if not domain_specified == initial_dot: raise AssertionError discard = False expires = expires == '' and None discard = True c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError('invalid Netscape format cookies file %r: %r' % (filename, line)) return
def _really_load(self, f, filename, ignore_discard, ignore_expires): try: import sqlite3 as sqlite from cookielib import _warn_unhandled_exception, LoadError, Cookie con = sqlite.connect(filename) query_str = 'select name, value, host_key, path, expires_utc, secure from cookies' for r in con.execute(query_str): name, value, host, path, expires, secure = r name = str(name) value = str(value) or None domain = str(host) initial_dot = domain.startswith(".") domain_specified = initial_dot path = str(path) or None path_specified = (path is not None) secure = (secure != 0) discard = False if not expires: expires = None discard = True c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, path_specified, secure, expires, discard, None, None, {}) if not ignore_expires and not c.is_expired(): self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Chrome sqlite format cookies file %r" % filename)
def _cookies_from_cursor(self, cursor): cookies = [] for row in cursor: domain = row[0] name = row[1] value = row[2] path = row[3] expires = row[4] secure = row[5] cookies.append(Cookie(None, name, value, None, None, domain, True, bool(domain.startswith(".")), path, True, secure, expires, False, None, None, None)) return cookies
def _add_cookie(self, domain, name, value, domain_specified="FALSE", path="/", secure="FALSE", expires=0): secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") # assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not self._ignore_discard and c.discard: #a session cookie #TODO deal with a session cookie pass if not self._ignore_expires and c.is_expired(): #end of life, do not add it raise RuntimeError( "the cookie's life ended, try add it in ignore_expires mod.") self.set_cookie(c) return
def _add_cookie(self, domain, name, value, domain_specified="FALSE", path="/", secure="FALSE", expires=0): secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") # assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not self._ignore_discard and c.discard: #a session cookie #TODO deal with a session cookie pass if not self._ignore_expires and c.is_expired(): #end of life, do not add it raise RuntimeError("the cookie's life ended, try add it in ignore_expires mod.") self.set_cookie(c) return
def _parse_cookie(self, lst): for item in lst: domain, domain_specified, path, path_specified, expires,\ name, value = item.split("\t") cookie = Cookie(0, name, value, None, False, domain, domain_specified.lower() == "true", domain.startswith("."), path, path_specified.lower() == "true", False, expires, False, None, None, {}) self._cookie.update({domain: {path: {name: cookie}}}) return self._cookie
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "%r does not look like a Netscape format cookies file" % filename) try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Netscape format cookies file %r: %r" % (filename, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): magic = f.readline() if not re.search(self.magic_re, magic): msg = '%r does not look like a Set-Cookie3 (LWP) format file' % filename raise LoadError(msg) now = time.time() header = 'Set-Cookie3:' boolean_attrs = ('port_spec', 'path_spec', 'domain_dot', 'secure', 'discard') value_attrs = ('version', 'port', 'path', 'domain', 'expires', 'comment', 'commenturl') try: while 1: line = f.readline() if line == '': break if not line.startswith(header): continue line = line[len(header):].strip() for data in split_header_words([line]): name, value = data[0] standard = {} rest = {} for k in boolean_attrs: standard[k] = False for k, v in data[1:]: if k is not None: lc = k.lower() else: lc = None if lc in value_attrs or lc in boolean_attrs: k = lc if k in boolean_attrs: if v is None: v = True standard[k] = v elif k in value_attrs: standard[k] = v else: rest[k] = v h = standard.get expires = h('expires') discard = h('discard') if expires is not None: expires = iso2time(expires) if expires is None: discard = True domain = h('domain') domain_specified = domain.startswith('.') c = Cookie(h('version'), name, value, h('port'), h('port_spec'), domain, domain_specified, h('domain_dot'), h('path'), h('path_spec'), h('secure'), expires, discard, h('comment'), h('commenturl'), rest) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError('invalid Set-Cookie3 format file %r: %r' % (filename, line)) return
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "The cookie does not appear to be in Netscape format. Check that the first line is: <br> # Netscape HTTP Cookie File") try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except AssertionError: raise AssertionError("Cookie does not follow Netscape format. Check that the values only are seperated by tab (\\t)" + self._parse_failed_cookie_message(line)) except Exception as ex: raise Exception(ex.message + self._parse_failed_cookie_message(line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): """ This function is required to monkey patch MozillaCookieJar's _really_load function which does not understand the curl format cookie file created by ecp-cookie-init. It patches the code so that #HttpOnly_ get loaded. https://bugs.python.org/issue2190 https://bugs.python.org/file37625/httponly.patch """ now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( "%r does not look like a Netscape format cookies file" % filename) try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] sline = line.strip() # support HttpOnly cookies (as stored by curl or old Firefox). if sline.startswith("#HttpOnly_"): line = sline[10:] # skip comments and blank lines XXX what is $ for? elif (sline.startswith(("#", "$")) or sline == ""): continue domain, domain_specified, path, secure, expires, name, value = \ line.split("\t") secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") if name == "": # cookies.txt regards 'Set-Cookie: foo' as a cookie # with no name, whereas cookielib regards it as a # cookie with no value. name = value value = None initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == "": expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Netscape format cookies file %r: %r" % (filename, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue # domain, domain_specified, path, secure, expires, name, value = \ # line.split("\t") arr = line.strip().split("\t") domain = arr.pop(0) domain_specified = arr.pop(0) path = arr.pop(0) secure = arr.pop(0) expires = arr.pop(0) name = arr.pop(0) if arr: value = arr.pop(0) else: value = None secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == None: expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Netscape format cookies file %r: %r" % (filename, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() try: while 1: line = f.readline() if line == "": break # last field may be absent, so keep any trailing tab if line.endswith("\n"): line = line[:-1] # skip comments and blank lines XXX what is $ for? if (line.strip().startswith(("#", "$")) or line.strip() == ""): continue # domain, domain_specified, path, secure, expires, name, value = \ # line.split("\t") # 2013-09-07 Firebug 导出的 cookies.txt 格式如下: # # .douban.fm TRUE / FALSE 1403077533 undefined openExpPan Y # # 和原来相比多了 undefined 这一列(PS: 不太清楚是干什么的) # 与 curl 使用的 cookie 文件格式不兼容,需要去掉这一列才能用于 curl arr = line.strip().split("\t") domain = arr.pop(0) domain_specified = arr.pop(0) path = arr.pop(0) secure = arr.pop(0) expires = arr.pop(0) value = arr.pop() if arr: name = arr.pop() else: name = value value = None secure = (secure == "TRUE") domain_specified = (domain_specified == "TRUE") initial_dot = domain.startswith(".") assert domain_specified == initial_dot discard = False if expires == None: expires = None discard = True # assume path_specified is false c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Netscape format cookies file %r: %r" % (filename, line))
def _really_load(self, f, filename, ignore_discard, ignore_expires): now = time.time() magic = f.readline() if not re.search(self.magic_re, magic): f.close() raise LoadError( '%r does not look like a Netscape format cookies file' % filename) try: while 1: line = f.readline() if line == '': break if line.endswith('\n'): line = line[:-1] if line.strip().startswith(('#', '$')) or line.strip() == '': continue domain, domain_specified, path, secure, expires, name, value = line.split( '\t') secure = secure == 'TRUE' domain_specified = domain_specified == 'TRUE' if name == '': name = value value = None initial_dot = domain.startswith('.') if not domain_specified == initial_dot: raise AssertionError discard = False expires = expires == '' and None discard = True c = Cookie(0, name, value, None, False, domain, domain_specified, initial_dot, path, False, secure, expires, discard, None, None, {}) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError('invalid Netscape format cookies file %r: %r' % (filename, line)) return
def _really_load(self, f, filename, ignore_discard, ignore_expires): magic = f.readline() if not re.search(self.magic_re, magic): msg = ("%r does not look like a Set-Cookie3 (LWP) format " "file" % filename) raise LoadError(msg) now = time.time() header = "Set-Cookie3:" boolean_attrs = ("port_spec", "path_spec", "domain_dot", "secure", "discard") value_attrs = ("version", "port", "path", "domain", "expires", "comment", "commenturl") try: while 1: line = f.readline() if line == "": break if not line.startswith(header): continue line = line[len(header):].strip() for data in split_header_words([line]): name, value = data[0] standard = {} rest = {} for k in boolean_attrs: standard[k] = False for k, v in data[1:]: if k is not None: lc = k.lower() else: lc = None # don't lose case distinction for unknown fields if (lc in value_attrs) or (lc in boolean_attrs): k = lc if k in boolean_attrs: if v is None: v = True standard[k] = v elif k in value_attrs: standard[k] = v else: rest[k] = v h = standard.get expires = h("expires") discard = h("discard") if expires is not None: expires = iso2time(expires) if expires is None: discard = True domain = h("domain") domain_specified = domain.startswith(".") c = Cookie(h("version"), name, value, h("port"), h("port_spec"), domain, domain_specified, h("domain_dot"), h("path"), h("path_spec"), h("secure"), expires, discard, h("comment"), h("commenturl"), rest) if not ignore_discard and c.discard: continue if not ignore_expires and c.is_expired(now): continue self.set_cookie(c) except IOError: raise except Exception: _warn_unhandled_exception() raise LoadError("invalid Set-Cookie3 format file %r: %r" % (filename, line))