def __call__(self, environ, start_response): """wsgi wrapper""" request = Request(environ, self.config._get('domain', '')) request.g = G() request.config = self.config request.qstr = QueryString(environ["QUERY_STRING"]) request.cookie = SimpleCookie(environ.get("HTTP_COOKIE","")) request.session = Session(rs=self.config.get('redis'), \ secret_key=self.config._get('secret_key', ''), \ cookie=request.cookie, expires=self.config._get('session_expires', 0)) #handle dispatch resp = self.url_dispatch(request.path, request) #cookie handle if (len(request.cookie)!=0): for k, v in request.cookie.items(): if '__utm' not in str(v): if not v['path']: v['path'] = '/' if not v['expires']: v['expires'] = self.config._get('cookie_expires', 0) request.headers.append( ('Set-Cookie', str(v).split(': ')[1]) ) #handle redirect if type(resp) is Redirect: request.status = resp.status request.headers.append(('Location', resp.location)) resp = '' elif type(resp) is HttpError: return resp #start resp request.headers.append(("Content-type", "text/html")) start_response(request.status, request.headers) if type(resp) is str: return resp elif type(resp) is unicode: return resp.encode('utf8') return ''.join([r.encode('utf8') for r in resp])
def _new_session(self): """ Start a new session with the IPA server by logging in and getting a cookie """ params = {"user": self._user, "password": self._password} reqheader = [] try: response = self._call_form_req("/ipa/session/login_password", params) # first make a note of when we got the session ID self._session_ts = time() c = SimpleCookie() c.load(response.getheader("set-cookie")) self._session_id = c["ipa_session"].value except IPAFormForbiddenError as e: if "x-ipa-rejection-reason" in e.headers: raise IPALoginError(e.headers["x-ipa-rejection-reason"]) else: raise IPALoginError("Unknown error")
def send(self, url, method="GET", **kwargs): """ XXX broken in pysaml2 1.0.2. Remove when upgrading """ _kwargs = copy.copy(self.request_args) if kwargs: _kwargs.update(kwargs) if self.cookiejar: _cd = self.cookies(url) if _cd: _kwargs["cookies"] = _cd if self.user and self.passwd: _kwargs["auth"] = (self.user, self.passwd) if "headers" in _kwargs and isinstance(_kwargs["headers"], list): if DICT_HEADERS: # requests.request wants a dict of headers, not a list of tuples _kwargs["headers"] = dict(_kwargs["headers"]) logger.debug("%s to %s" % (method, url)) for arg in ["cookies", "data", "auth"]: try: logger.debug("%s: %s" % (arg.upper(), _kwargs[arg])) except KeyError: pass r = requests.request(method, url, **_kwargs) logger.debug("Response status: %s" % r.status_code) try: self.set_cookie(SimpleCookie(r.headers["set-cookie"]), r) except (AttributeError, KeyError): pass return r
def update_session_from_browser(self): """ Updtate our session from the browser cookies. We want to limit access to the actual session cookies, as their name in the browser may differ from how the value is named on our session representation, which is loosely based on that of RV itself. """ self._logger.debug("Updating session cookies from browser") cookie_jar = self._view.page().networkAccessManager().cookieJar() # Here, the cookie jar is a dictionary of key/values cookies = SimpleCookie() for cookie in cookie_jar.allCookies(): cookies.load(str(cookie.toRawForm())) encoded_cookies = _encode_cookies(cookies) content = { "session_expiration": get_saml_claims_expiration(encoded_cookies), "session_id": get_session_id(encoded_cookies), "user_id": get_user_name(encoded_cookies), "csrf_key": get_csrf_key(encoded_cookies), "csrf_value": get_csrf_token(encoded_cookies), } # To minimize handling, we also keep a snapshot of the browser cookies. # We do so for all of them, as some are used by the IdP and we do not # want to manage those. Their names may change from IdP version and # providers. We figure it is simpler to keep everything. # Here, we have a list of cookies in raw text form content["cookies"] = encoded_cookies self._session.merge_settings(content)
def __getsid(self): """Get the current session ID or return a new one""" # first, try to load the sid from the GET or POST forms #query_string = sys.stdin.read() #parser = FormParser() #parser.parse_values(query_string)#query_string.partition('&') #_S_ID = parser.get_value("S_ID", "") #if S_ID: # sid = S_ID # return sid # then try to load the sid from the HTTP cookie self.cookie = SimpleCookie() if os.environ.has_key('HTTP_COOKIE'): self.cookie.load(os.environ['HTTP_COOKIE']) if S_ID in self.cookie: sid = self.cookie[S_ID].value return sid else: raise NoCookiesError("Could not find any cookies") # if all else fails, return a new sid return self.__newsid()
def __init__(self): self.sid = None try: cookies = SimpleCookie(os.environ['HTTP_COOKIE']) except KeyError: cookies = {} try: sid = cookies['sid'].value except KeyError: self.sid = self.remake_sid() return try: self.verify_sid(sid) except ValueError: self.sid = self.remake_sid() return # Beyond this point the value of 'sid' is trusted. if not os.access(Settings.tempdir+"/"+sid, os.W_OK | os.X_OK): self.sid = self.remake_sid() return self.sid = sid
def get_session(self, environ): """ Returns the session whose id is stored in the cookie or None if there is no cookie. """ try: cookie = SimpleCookie(environ['HTTP_COOKIE']) session_id = cookie['session_id'].value cur = environ['db'].query( 'SELECT SES_EXPIRES, SES_IS_ADMIN FROM SESSIONS \ WHERE SES_ID = ?;', session_id) result = cur.fetchonemap() if not result: return None expiration = result['SES_EXPIRES'] is_admin = bool(result['SES_IS_ADMIN']) session = Session(session_id, expiration, is_admin) if expiration < datetime.now(): logger.info('session expired: %s' % session_id) session.delete(environ) session = None logger.info('loaded session: %s' % session_id) return session except KeyError, e: return None
def requester(target, cookie, user_agent, proxy, match, filename): try: url = target + filename proxy_dict = {} cookie_dict = {} if proxy != "": proxy_dict = {"http": proxy, "https": proxy} # If the user specified a cookie convert it to a requests acceptable dict if cookie != "": cookie_dict = SimpleCookie() cookie_dict.load(cookie) cookie = {} for key, morse1 in cookie_dict.items(): cookie[key] = morse1.value # If the user want to match a specific string in the response if match != "": req = requests.get(url, cookies=cookie, headers={"User-Agent": user_agent}, proxies=proxy_dict, verify=False) requester.q.put([url, req.status_code, req.content]) else: req = requests.head(url, cookies=cookie, headers={"User-Agent": user_agent}, proxies=proxy_dict, verify=False) requester.q.put([url, req.status_code]) except KeyboardInterrupt: sys.stdout = open(os.devnull, 'w')
def parse(self, response): set_cookie = response.headers.getlist('Set-Cookie') if len(set_cookie[0]) > len(set_cookie[1]): self.cookie += set_cookie[0].split(' ')[0] else: self.cookie += set_cookie[1].split(' ')[0] self.cookie = { i.key: i.value for i in SimpleCookie(self.cookie).values() } url = 'https://data.xiaodianpu.com/api/hotword-service/home/v1/wordList?' param_data = { 'showType': 0, 'categoryId': -1, 'page': 1, 'pageSize': 10 } data = urllib.urlencode(param_data) # for i in range(1, 11): # param_data['page'] = i # yield scrapy.Request( url + data , callback=self.parse_item) yield scrapy.Request(url + data, cookies=self.cookie, callback=self.parse_word_List)
def COOKIES(self): if not self._COOKIES: self._COOKIES = SimpleCookie() return self._COOKIES
cursor.close() connector.close() try: # Decrypt secret_key = inifile.get('pycrypto', 'key') crypto = AES.new(secret_key) hx = rec[0][2] cipher_data = crypto.decrypt(hx.decode('hex')) cip = cipher_data.replace(" ", "") fw.write("\n%s" % str(cip)) if len(rec) > 0 and p == cip: response = "true" c = SimpleCookie() expires = datetime.datetime.now() + datetime.timedelta(hours=1) c['auth'] = u c['auth']['expires'] = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT") c['auth']['path'] = '/' fw.write("\n%s" % str(c.output())) print c else: response = "false" except: response = "false" finally: fw.write("\n%s" % str(response))
def login(self, environ, start_response): print environ['wsgi.input'] form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) returnStatement = "logged in" try: username = form['username'].value except KeyError: username = None try: password = form['password'].value except KeyError: password = None k = '' v = '' conn = sqlite3.connect('meep.db') c = conn.cursor() if username and password: c.execute("SELECT * FROM Users WHERE username = '******' AND password = '******'") if c.fetchone(): k = 'Location' v = '/main_page' returnStatement = "logged in" meeplib.set_curr_user(username) else: k = 'Location' v = '/' returnStatement = """<p>Invalid user. Please try again.</p>""" # Test whether variable is defined to be None #if username is not None: # if password is not None: # if meeplib.check_user(username, password) is False: # k = 'Location' # v = '/' # returnStatement = """<p>Invalid user. Please try again.</p>""" # # else: # new_user = meeplib.User(username, password) # meeplib.set_curr_user(username) # k = 'Location' # v = '/main_page' # else: # returnStatement = """<p>password was not set. User could not be created</p>""" #else: # returnStatement = """<p>username was not set. User could not be created</p>""" #print """isValidafter: %s """ %(meeplib.check_user(username, password),) # set content-type headers = [('Content-type', 'text/html')] if returnStatement is "logged in": c = SimpleCookie() cookie_name, cookie_val = make_set_cookie_header('username', username) headers.append((cookie_name, cookie_val)) print cookie_name + cookie_val headers.append((k, v)) start_response('302 Found', headers) print "everything seems to be working up till now..." #return self.main_page(environ, start_response) return returnStatement
def confirm(theform, userdir, thisscript): """Confirm a login. Either from an invite or from a user who has registered.""" from modules.dataenc import pass_dec, pass_enc from login import encodestring fail = False try: theval, daynumber, timestamp = pass_dec(theform['id'].value) except: # FIXME: bare except.... newloginfail() tempstore = ConfigObj(userdir + 'temp.ini') if not tempstore.has_key(theval): newloginfail() uservals = tempstore[theval] del tempstore[theval] username = uservals['username'] if username in tempstore['pending']: tempstore['pending'].remove(username) tempstore.write() # newconfig = ConfigObj(userdir + 'default.ini') newpath = userdir + username + '.ini' if os.path.isfile(newpath): newloginfail() newconfig.filename = newpath # FIXME: should this be '' ? action = None for entry in uservals: if entry == 'action': action = uservals[entry] elif entry == 'password': password = uservals[entry] pwd_hash = pwd_context.hash(password, salt="") newconfig[entry] = pass_enc(pwd_hash, timestamp=True, daynumber=True) else: newconfig[entry] = uservals[entry] newconfig.write() # # next we need to create the cookie header to return it from Cookie import SimpleCookie thecookie = SimpleCookie() pwd_hash = pwd_context.hash(password, salt="") thecookie['userid'] = encodestring(newconfig['username'], pwd_hash) config = ConfigObj(userdir + 'config.ini') maxage = newconfig['max-age'] cookiepath = config['cookiepath'] if maxage and int( maxage ): # possible cause of error here if the maxage value in a users file isn't an integer !! thecookie['userid']['max-age'] = int(maxage) if cookiepath: thecookie['userid']['path'] = cookiepath if config['adminmail']: msg = 'A new user has created a login - "%s".\n\n' % thisscript for entry in newconfig: if entry != 'password': msg += entry + ' : ' + newconfig[entry] + '\n' # FIXME: should be mailme sendmailme(config['adminmail'], msg, config['email_subject'], config['adminmail'], html=False) return action, newconfig, thecookie.output()
def set_cookies(environ): cookie = SimpleCookie() cookie['page_visits'] = retrieve_visits(environ) cookie_headers = ('Set-Cookie', cookie['page_visits'].OutputString()) headers = [cookie_headers, HEADER] return headers
def get_crumbs(): docCookie = doc().cookie c = SimpleCookie(docCookie) c = c.output(header='') return map(strip, c.split('\n'))
def test_cookie_not_permanent(self): """Check that by default the visit cookie is not permanent.""" response = self.app.get('/') cookies = SimpleCookie(response.headers['Set-Cookie']) morsel = cookies[self.cookie_name] assert not morsel['expires'] and not morsel['max-age']
#!/usr/bin/env python2 from pprint import pprint import cgi import cgitb import sqlite3 from Cookie import SimpleCookie from subprocess import call from html_helpers import header, footer cgitb.enable() print 'Content-type: text/html\n' conn = sqlite3.connect('insta.db') cursor = conn.cursor() cookie = SimpleCookie() admin_exist = cursor.execute( 'SELECT 1=1 FROM users WHERE username = "******"').fetchone() != None header() print '<h1>System Initialization</h1><p>Important: all data would be deleted</p>' \ '<form method="POST" action="/cgi-bin/reset.py">' if admin_exist: print '<div class="form-group"><input class="form-control" placeholder="Enter admin password" name="password_verify" required type=password></div>' else: print '<div class="form-group"><input class="form-control" placeholder="Define admin password" name="password_new" required type=password></div>' print '<button name="confirm" value="yes" class="btn btn-danger btn-lg btn-block">Please Go Ahead</button>' \
def __init__(self, sock, method="GET", scheme="http", path="/", protocol=(1, 1), qs="", headers=None, server=None): "initializes x; see x.__class__.__doc__ for signature" self.sock = sock self.method = method self.scheme = scheme or Request.scheme self.path = path self.protocol = protocol self.qs = qs self.print_debug = getattr(server, "display_banner", False) self.headers = headers or Headers() self.server = server self.cookie = SimpleCookie() if sock is not None: name = sock.getpeername() try: ip, port = name name = None except ValueError: # AF_UNIX ip, port = None, None self.remote = Host(ip, port, name) cookie = self.headers.get("Cookie") if cookie is not None: self.cookie.load(cookie) self.body = BytesIO() if self.server is not None: self.local = Host(self.server.host, self.server.port) try: host = self.headers["Host"] if ":" in host: parts = host.split(":", 1) host = parts[0] port = int(parts[1]) else: port = 443 if self.scheme == "https" else 80 except KeyError: host = self.local.name or self.local.ip port = getattr(self.server, "port") self.host = host self.port = port base = "{0:s}://{1:s}{2:s}/".format( self.scheme, self.host, ":{0:d}".format(self.port) if self.port not in (80, 443) else "") self.base = parse_url(base) url = "{0:s}{1:s}{2:s}".format( base, self.path, "?{0:s}".format(self.qs) if self.qs else "") self.uri = parse_url(url) self.uri.sanitize()
def test_404_with_mamo_cookie(self): self.client.cookies = SimpleCookie({'mamo': 'on'}) res = self.client.get('/en-US/firefox/xxxxxxx') assert res.status_code == 404 self.assertTemplateUsed(res, 'amo/404-responsive.html')
def delete_cookie(self, key): if self._cookies is None: self._cookies = SimpleCookie() if key not in self._cookies: self._cookies[key] = '' self._cookies[key]['max-age'] = 0
def __init__(self): self.cookies = SimpleCookie()
def track(env, start_response): try: gaid, domain = env['PATH_INFO'].lstrip('/').split('/', 1) if '/' in domain: raise ValueError except ValueError: start_response('400', []) return """ <html><body> <h1>Usage:</h1> <code>%(host)s/UA-123456/domain.com?dp=<b>/path/to/page</b>&t=pageview<i>&args</i></code> <blockquote><dl> <dt><strong>UA-123456</strong></dt> <dd>This is the Tracking ID</dd> <dt><strong>domain.com</strong></dt> <dd>This is the domain that you see in your <a href="https://support.google.com/analytics/answer/2790010?hl=en-GB"> Universal Analytics</a> javascript tracking code.</dd> <dt><strong>t</strong></dt> <dd>Type of event.</dd> <dt><strong>dp</strong></dt> <dd>Page url that should show as viewed. You want this to be the page path that the feed item points to. In other words, the URL without the protocol and domain. <strong>Don't forget to quote it !</strong> </dd> <dt><strong>dr</strong></dt> <dd>You can specify a different referral URL here. If you don't specify this then the <code>Referer</code> header is used. Set <code>dr</code> to empty value if you don't want this. <strong>Don't forget to quote it !</strong> </dd> <dt><strong>referer</strong></dt> <dd>Specify this to override the value from the <code>Referer</code> header is used. Has no effect if <strong><code>dr</code></strong> is set. <strong>Don't forget to quote it !</strong> </dd> <dt><strong>protocol</strong></dt> <dd>Specify this to set a specific protocol in the redirect. Acceptable values: http, https </dd> <dt><strong><em>args</em></strong></dt> <dd>Any <a href="https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters"> measurement protocol</a> parameters.</dd> </blockquote> </body></html> """ % {'host': env.get('HTTP_HOST', '')} cookies = SimpleCookie() if 'HTTP_COOKIE' in env: cookies.load(env['HTTP_COOKIE']) cid = 'cid' in cookies and cookies['cid'].value or str(uuid4()) cookies['cid'] = cid cookies['cid']['path'] = '/%s/%s' % (gaid, domain) cookies['cid']['max-age'] = 62899200 parameters = url_decode(env["QUERY_STRING"]) referer = parameters.pop('referer', None) protocol = parameters.pop('protocol', None) assert protocol in (None, 'http', 'https') data = dict( v=1, tid=gaid, cid=cid, dh=domain, dr=env.get("HTTP_REFERER", '') if referer is None else referer, z=str(time()), ) data.update(parameters.items()) logging.info("Redirect data: %r", data) location = protocol + ":" if protocol else "" location += "//www.google-analytics.com/collect?" + urlencode(data) #start_response('200', [ # ('Set-Cookie', cookies['cid'].OutputString()), #]) #return '<a href="%s?%s">x</a><code>%r</code><br><iframe src="%s"/>' % ( # env['PATH_INFO'], env["QUERY_STRING"], # location, location #) start_response('307 Temporary Redirect', [ ('Set-Cookie', cookies['cid'].OutputString()), ('Location', location.encode('utf8')) ]) return ""
#!/usr/bin/env python2 from pprint import pprint import cgi import cgitb import sqlite3 from Cookie import SimpleCookie import os from html_helpers import header, footer cgitb.enable() print 'Content-type: text/html\n' form = cgi.FieldStorage() conn = sqlite3.connect('insta.db') cursor = conn.cursor() cursor.execute("PRAGMA foreign_keys = ON") cookie = SimpleCookie(os.environ['HTTP_COOKIE']) if form['password'].value != form['password2'].value: print 'The passwords does not match' else: if cursor.execute( 'UPDATE users SET `password` = ? WHERE `username` = ? AND `password` = ?', (form['password'].value, cookie['username'].value, form['password_old'].value)).rowcount >= 1: conn.commit() print 'Nice! Updated.' else: print 'Your current password is incorrect'
def __call__(self, *args, **vars): "Perform a GET/POST request and return the response" location = self.location if isinstance(location, unicode): location = location.encode("utf8") # extend the base URI with additional components if args: location += "/".join(args) if self.method == "GET": location += "?%s" % urlencode(vars) # prepare the request content suitable to be sent to the server: if self.enctype == "multipart/form-data": boundary, body = self.multipart_encode(vars) content_type = '%s; boundary=%s' % (self.enctype, boundary) elif self.enctype == "application/x-www-form-urlencoded": body = urlencode(vars) content_type = self.enctype else: body = None # add headers according method, cookies, etc.: headers = {} if self.method == "POST": headers.update({ 'Content-type': content_type, 'Content-length': str(len(body)), }) if self.cookies: headers['Cookie'] = self.cookies.output(attrs=(), header="", sep=";") if self.referer: headers['Referer'] = self.referer if self.trace: print "-" * 80 print "%s %s" % (self.method, location) print '\n'.join(["%s: %s" % (k, v) for k, v in headers.items()]) print "\n%s" % body # send the request to the server and store the result: response, content = self.http.request(location, self.method, body=body, headers=headers) self.response = response self.content = content if self.trace: print print '\n'.join(["%s: %s" % (k, v) for k, v in response.items()]) print content print "=" * 80 # Parse and store the cookies (if any) if "set-cookie" in self.response: if not self.cookies: self.cookies = SimpleCookie() self.cookies.load(self.response["set-cookie"]) return content
class HTTPBase(object): def __init__(self, verify=True, ca_bundle=None, key_file=None, cert_file=None): self.request_args = {"allow_redirects": False} #self.cookies = {} self.cookiejar = cookielib.CookieJar() self.request_args["verify"] = verify if verify: if ca_bundle: self.request_args["verify"] = ca_bundle if key_file: self.request_args["cert"] = (cert_file, key_file) self.sec = None self.user = None self.passwd = None def cookies(self, url): """ Return cookies that are matching the path and are still valid :param url: :return: """ part = urlparse.urlparse(url) #if part.port: # _domain = "%s:%s" % (part.hostname, part.port) #else: _domain = part.hostname cookie_dict = {} now = utc_now() for _, a in list(self.cookiejar._cookies.items()): for _, b in a.items(): for cookie in list(b.values()): # print cookie if cookie.expires and cookie.expires <= now: continue if not re.search("%s$" % cookie.domain, _domain): continue if not re.match(cookie.path, part.path): continue cookie_dict[cookie.name] = cookie.value return cookie_dict def set_cookie(self, kaka, request): """Returns a cookielib.Cookie based on a set-cookie header line""" if not kaka: return part = urlparse.urlparse(request.url) _domain = part.hostname logger.debug("%s: '%s'" % (_domain, kaka)) for cookie_name, morsel in kaka.items(): std_attr = ATTRS.copy() std_attr["name"] = cookie_name _tmp = morsel.coded_value if _tmp.startswith('"') and _tmp.endswith('"'): std_attr["value"] = _tmp[1:-1] else: std_attr["value"] = _tmp std_attr["version"] = 0 # copy attributes that have values for attr in morsel.keys(): if attr in ATTRS: if morsel[attr]: if attr == "expires": std_attr[attr] = _since_epoch(morsel[attr]) elif attr == "path": if morsel[attr].endswith(","): std_attr[attr] = morsel[attr][:-1] else: std_attr[attr] = morsel[attr] else: std_attr[attr] = morsel[attr] elif attr == "max-age": if morsel["max-age"]: std_attr["expires"] = time.time() + int( morsel["max-age"]) for att, item in PAIRS.items(): if std_attr[att]: std_attr[item] = True if std_attr["domain"]: if std_attr["domain"].startswith("."): std_attr["domain_initial_dot"] = True else: std_attr["domain"] = _domain std_attr["domain_specified"] = True if morsel["max-age"] is 0: try: self.cookiejar.clear(domain=std_attr["domain"], path=std_attr["path"], name=std_attr["name"]) except ValueError: pass elif morsel["expires"] < utc_now(): try: self.cookiejar.clear(domain=std_attr["domain"], path=std_attr["path"], name=std_attr["name"]) except ValueError: pass else: new_cookie = cookielib.Cookie(**std_attr) self.cookiejar.set_cookie(new_cookie) def send(self, url, method="GET", **kwargs): _kwargs = copy.copy(self.request_args) if kwargs: _kwargs.update(kwargs) if self.cookiejar: _cd = self.cookies(url) if _cd: _kwargs["cookies"] = _cd if self.user and self.passwd: _kwargs["auth"] = (self.user, self.passwd) if "headers" in _kwargs and isinstance(_kwargs["headers"], list): if DICT_HEADERS: # requests.request wants a dict of headers, not a list of tuples _kwargs["headers"] = dict(_kwargs["headers"]) try: logger.debug("%s to %s" % (method, url)) for arg in ["cookies", "data", "auth"]: try: logger.debug("%s: %s" % (arg.upper(), _kwargs[arg])) except KeyError: pass r = requests.request(method, url, **_kwargs) logger.debug("Response status: %s" % r.status_code) except requests.ConnectionError, exc: raise ConnectionError("%s" % exc) try: self.set_cookie(SimpleCookie(r.headers["set-cookie"]), r) except AttributeError: pass except KeyError: pass return r
def set_cookie(self, request): """Translate request's cookie into a SimpleCookie at self.cookie. """ raw_cookie = request.message.get('Cookie', '') self.cookie = SimpleCookie(raw_cookie)
def COOKIES(self): """ A dict-like SimpleCookie instance. Use :meth:`set_cookie` instead. """ if not self._COOKIES: self._COOKIES = SimpleCookie() return self._COOKIES
def convertAsync(self,config): if (config != None): config['clientName'] = "PYTHON" config['clientVersion'] = PDFreactor.VERSION; url = self.url + "/convert/async.json" if (self.apiKey != None): url += '?apiKey=' + self.apiKey result = "" if len(self.__headers.keys()) == False: headers = self.__headers else : headers = {} for (key, value) in self.__headers.items(): lcKey = key.lower() if lcKey != "content-type" and lcKey != "range" and lcKey != "user-agent": headers[key] = value headers['Content-Type'] = 'application/json' headers['Cookie'] = '; '.join(['%s=%s' % (key, value) for (key, value) in self.__cookies.items()]) headers['User-Agent'] = 'PDFreactor Python API v4' headers['X-RO-User-Agent'] = 'PDFreactor Python API v4' req = None if sys.version_info[0] == 2: from urllib2 import HTTPError else: from urllib.error import HTTPError try: if sys.version_info[0] == 2: import Cookie from Cookie import SimpleCookie import urllib2 from urllib2 import URLError options = json.dumps(config) req = urllib2.Request(url, options, headers) response = urllib2.urlopen(req) else: import http.cookies from http.cookies import SimpleCookie import urllib.request options = json.dumps(config) req = urllib.request.Request(url, options.encode(), headers) response = urllib.request.urlopen(req) except HTTPError as e: if (e.code == 422): raise Exception(json.loads(e.read())['error']) elif (e.code == 400): raise Exception('Invalid client data. ' + json.loads(e.read())['error']) elif (e.code == 404): raise Exception('Error connecting to PDFreactor Web Service at ' + self.url + '. Please make sure the PDFreactor Web Service is installed and running ' + json.loads(e.read())['error']) elif (e.code == 403): raise Exception('Request rejected. ' + json.loads(e.read())['error']) elif (e.code == 401): raise Exception('Unauthorized. ' + json.loads(e.read())['error']) elif (e.code == 413): raise Exception('The configuration is too large to process.') elif (e.code == 500): raise Exception(json.loads(e.read())['error']) elif (e.code == 503): raise Exception('Asynchronous conversions are unavailable.') elif (e.code > 400): raise Exception('PDFreactor Web Service error (status: ' + str(e.code) + ').') except Exception as e: raise Exception('Error connecting to PDFreactor Web Service at ' + self.url + '. Please make sure the PDFreactor Web Service is installed and running (Error: ' + str(e.reason) + ')') documentId = None; if (response != None and response.info() != None): location = response.info().getheader("Location") if (location != None): documentId = location[location.rfind("/")+1:len(location)] cookieHeader = response.info().getheader("Set-Cookie") if (cookieHeader != None): self.stickyMap[documentId] = {'cookies': {}, 'keepDocument': config['keepDocument'] if ('keepDocument' in config) else False} cookies = SimpleCookie() cookies.load(cookieHeader) for name in cookies: self.stickyMap[documentId]['cookies'][name] = cookies[name].value return documentId
def _extract_response(page, encoding='utf8'): history = [] set_cookies = [] res = None try: for i, url in enumerate(page['history']): resource = page['resources'].pop(0) while resource['request']['url'] != url: resource = page['resources'].pop(0) if resource['error']: return resource['error'], None request = resource['request'] req = PreparedRequest() req.method = request['method'].encode(encoding) req.url = request['url'].encode(encoding) # Set Request Headers req.headers = CaseInsensitiveDict() for header in request['headers']: req.headers[header['name'].encode(encoding)] = header['value'].encode(encoding) # Set Request Cookies req._cookies = RequestsCookieJar() if set_cookies: if 'Cookie' not in req.headers: req.headers['Cookie'] = "" else: set_cookies.insert(0, '') req.headers['Cookie'] += "; ".join(set_cookies) if 'Cookie' in req.headers: cookies = SimpleCookie() cookies.load(req.headers['Cookie']) for key, cookie in cookies.items(): req._cookies.set(key, cookie.value) req.body = request.get('postData', None) if req.body: req.body = req.body.encode(encoding) response = resource['endReply'] or resource['startReply'] res = Response() res.encoding = encoding res.url = response['url'].encode(encoding) res.status_code = response['status'] for header in response['headers']: res.headers[header['name'].encode(encoding)] = header['value'].encode(encoding) if header['name'] == 'Set-Cookie': set_cookies.append(res.headers[header['name']].rsplit(';', 1)[0]) res.history = list(history) res.request = req history.append(res) res._content = re.sub( ( '<html><head></head><body>' '<pre style="word-wrap: break-word; white-space: pre-wrap;">(.*?)</pre>' '</body></html>' ), r'\1', page['content'], flags=re.DOTALL ).encode(encoding) return None, res except IndexError: return {'errorCode': -1, 'errorString': 'An existing connection was forcibly closed by the remote host'}, None
def clear_cookies(self): self._cookies = SimpleCookie()