def wrap(request, *args, **kwargs): if 'token' not in request.GET and 'token' not in request.session: # no token at all, request one-time-token # next: where to redirect # scope: what service you want to get access to base_url='https://www.google.com/accounts/AuthSubRequest' scope='https://docs.google.com/feeds/%20https://docs.googleusercontent.com/' urlbase = Site.objects.get_current().domain next_url='http://%s%s' % (urlbase, request.get_full_path()) session_val='1' target_url="%s?next=%s&scope=%s&session=%s" % (base_url, next_url, scope, session_val) return HttpResponseRedirect(target_url) elif 'token' not in request.session and 'token' in request.GET: # request session token using one-time-token conn = HTTPSConnection("www.google.com") conn.putrequest('GET', '/accounts/AuthSubSessionToken') conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET['token']) conn.endheaders() conn.send(' ') r = conn.getresponse() if str(r.status) == '200': token = r.read() token = token.split('=')[1] token = token.replace('', '') request.session['token'] = token return f(request, *args, **kwargs)
def wrap(request, *args, **kwargs): if 'token' not in request.GET and 'token' not in request.session: # no token at all, request one-time-token # next: where to redirect # scope: what service you want to get access to base_url='https://www.google.com/accounts/AuthSubRequest' scope='https://docs.google.com/feeds/%20https://docs.googleusercontent.com/' next_url='http://ilsgateway.com%s' % request.get_full_path() session_val='1' target_url="%s?next=%s&scope=%s&session=%s" % (base_url, next_url, scope, session_val) return HttpResponseRedirect(target_url) elif 'token' not in request.session and 'token' in request.GET: # request session token using one-time-token conn = HTTPSConnection("www.google.com") conn.putrequest('GET', '/accounts/AuthSubSessionToken') conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET['token']) conn.endheaders() conn.send(' ') r = conn.getresponse() if str(r.status) == '200': token = r.read() token = token.split('=')[1] token = token.replace('', '') request.session['token'] = token return f(request, *args, **kwargs)
def test_post_multipart(self): alphabet = "abcdefghijklmnopqrstuvwxyz" # generate file contents for a large post contents = "".join([c * 65536 for c in alphabet]) # encode as multipart form data files=[('file', 'file.txt', contents)] content_type, body = encode_multipart_formdata(files) # post file if self.scheme == 'https': c = HTTPSConnection('%s:%s' % (self.interface(), self.PORT)) else: c = HTTPConnection('%s:%s' % (self.interface(), self.PORT)) c.putrequest('POST', '/post_multipart') c.putheader('Content-Type', content_type) c.putheader('Content-Length', str(len(body))) c.endheaders() c.send(body) response = c.getresponse() self.body = response.fp.read() self.status = str(response.status) self.assertStatus(200) self.assertBody(", ".join(["%s * 65536" % c for c in alphabet]))
def post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = HTTPSConnection(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body.encode('utf-8')))) h.putheader('User-Agent', 'sms-python') h.endheaders() h.send(body.encode('utf-8')) resp = h.getresponse() return resp.status, resp.reason, resp.read().decode()
def post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = HTTPSConnection(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body.encode('utf-8')))) h.putheader('User-Agent', 'sms-python') h.endheaders() h.send(body.encode('utf-8')) resp = h.getresponse() return resp.status, resp. reason, resp.read().decode()
def get_word(word): url = 'https://dictionary.cambridge.org/dictionary/english/%s' % (word) con = HTTPSConnection('dictionary.cambridge.org') con.connect() con.request('GET', url) con.send('') resp = con.getresponse() resp.begin() doc = resp.read().decode('utf-8') word = CambridgeWord(doc) return word
def zopeRequest(self, method, headers={}, body=''): """Send a request back to Zope""" try: if self.ssl: h = HTTPSConnection(self.host) else: h = HTTPConnection(self.host) h.putrequest(method, self.path) h.putheader('User-Agent', 'Zope External Editor/%s' % __version__) h.putheader('Connection', 'close') for header, value in headers.items(): h.putheader(header, value) h.putheader("Content-Length", str(len(body))) if self.metadata.get('auth','').startswith('Basic'): h.putheader("Authorization", self.metadata['auth']) if self.metadata.get('cookie'): h.putheader("Cookie", self.metadata['cookie']) h.endheaders() h.send(body) return h.getresponse() except: # On error return a null response with error info class NullResponse: def getheader(self, n, d=None): return d def read(self): return '(No Response From Server)' response = NullResponse() response.reason = sys.exc_info()[1] try: response.status, response.reason = response.reason except ValueError: response.status = 0 if response.reason == 'EOF occurred in violation of protocol': # Ignore this protocol error as a workaround for # broken ssl server implementations response.status = 200 return response
def test_page1(request): """ Simple example - list google docs documents """ if TOKEN_VAR in request.session: con = HTTPSConnection("www.google.com") con.putrequest('GET', '/m8/feeds/contacts/[email protected]/full') con.putheader('Authorization', 'AuthSub token="%s"' % request.session[TOKEN_VAR]) con.endheaders() con.send('') r2 = con.getresponse() dane = r2.read() soup = BeautifulStoneSoup(dane) dane = soup.prettify() return render(request, 'pin/a.html', {'dane': dane}) else: return render(request, 'pin/a.html', {'dane': 'bad bad'})
def request(method, url, data, headers, callback=None): url = urlparse(url) # Connect if url.scheme == 'https': request = HTTPSConnection(url.netloc) else: request = HTTPConnection(url.netloc) request.connect() # Initiate request request.putrequest(method, url.path) encoded_data = multipart.encode(data) encoded_data_headers = encoded_data.get_headers() all_headers = chain( encoded_data_headers.iteritems(), headers.iteritems() ) # Send headers for name, value in all_headers: request.putheader(name, value) request.endheaders() # Send body bytes_sent = 0 bytes_total = int(encoded_data_headers['Content-Length']) for chunk in encoded_data: request.send(chunk) bytes_sent += len(chunk) if callable(callback): callback(bytes_sent, bytes_total) # TODO: Wrap the response in a container to allow chunked reading. response = request.getresponse() response_status = response.status response_data = response.read() request.close() return response_status, response_data
def run(self): self.started = time.time() timeout = self.hostControlBackend._hostRpcTimeout if timeout < 0: timeout = 0 try: query = toJson({ 'id': 1, 'method': self.method, 'params': self.params }).encode('utf-8') connection = HTTPSConnection(host=self.address, port=self.hostPort, timeout=timeout) with closingConnection(connection) as connection: non_blocking_connect_https(connection, timeout) connection.putrequest('POST', '/opsiclientd') connection.putheader('User-Agent', self._USER_AGENT) connection.putheader('content-type', 'application/json') connection.putheader('content-length', str(len(query))) auth = u'{0}:{1}'.format(self.username, self.password) connection.putheader( 'Authorization', 'Basic ' + base64.b64encode(auth.encode('latin-1'))) connection.endheaders() connection.send(query) response = connection.getresponse() response = response.read() response = fromJson(unicode(response, 'utf-8')) if response and isinstance(response, dict): self.error = response.get('error') self.result = response.get('result') else: self.error = u"Bad response from client: %s" % forceUnicode( response) except Exception as e: self.error = forceUnicode(e) finally: self.ended = time.time()
def zope_request(self, method, headers={}, body=''): """Send a request back to Zope""" try: if self.ssl: h = HTTPSConnection(self.host) else: h = HTTPConnection(self.host) h.putrequest(method, self.path) #h.putheader("Host", self.host) # required by HTTP/1.1 h.putheader('User-Agent', 'Zope External Editor/%s' % __version__) h.putheader('Connection', 'close') for header, value in headers.items(): h.putheader(header, value) h.putheader("Content-Length", str(len(body))) if self.metadata.get('auth', '').startswith('Basic'): h.putheader("Authorization", self.metadata['auth']) if self.metadata.get('cookie'): h.putheader("Cookie", self.metadata['cookie']) h.endheaders() h.send(body) return h.getresponse() except: # On error return a null response with error info class NullResponse: def getheader(n, d): return d def read(): return '' response = NullResponse() response.reason = sys.exc_info()[1] try: response.status, response.reason = response.reason except: response.status = 0 return response
def wrap(request, *args, **kwargs): if TOKEN_IN_GET not in request.GET and TOKEN_VAR not in request.session: # no token at all, request one-time-token # next: where to redirect # scope: what service you want to get access to return HttpResponseRedirect("https://www.google.com/accounts/AuthSubRequest?next=http://127.0.0.1:8000/pin/test_page&scope=https://www.google.com/m8/feeds&session=1") elif TOKEN_VAR not in request.session and TOKEN_IN_GET in request.GET: # request session token using one-time-token conn = HTTPSConnection("www.google.com") conn.putrequest('GET', '/accounts/AuthSubSessionToken') conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET[TOKEN_IN_GET]) conn.endheaders() conn.send(' ') r = conn.getresponse() if str(r.status) == '200': token = r.read() token = token.split('=')[1] token = token.replace('', '') request.session[TOKEN_VAR] = token return f(request, *args, **kwargs)
def zope_request(self, method, headers={}, body=''): """Send a request back to Zope""" try: if self.ssl: h = HTTPSConnection(self.host) else: h = HTTPConnection(self.host) h.putrequest(method, self.path) #h.putheader("Host", self.host) # required by HTTP/1.1 h.putheader('User-Agent', 'Zope External Editor/%s' % __version__) h.putheader('Connection', 'close') for header, value in headers.items(): h.putheader(header, value) h.putheader("Content-Length", str(len(body))) if self.metadata.get('auth','').startswith('Basic'): h.putheader("Authorization", self.metadata['auth']) if self.metadata.get('cookie'): h.putheader("Cookie", self.metadata['cookie']) h.endheaders() h.send(body) return h.getresponse() except: # On error return a null response with error info class NullResponse: def getheader(n,d): return d def read(): return '' response = NullResponse() response.reason = sys.exc_info()[1] try: response.status, response.reason = response.reason except: response.status = 0 return response
def wrap(request, *args, **kwargs): if TOKEN_IN_GET not in request.GET and TOKEN_VAR not in request.session: # no token at all, request one-time-token # next: where to redirect # scope: what service you want to get access to return HttpResponseRedirect( "https://www.google.com/accounts/AuthSubRequest?next=http://127.0.0.1:8000/pin/test_page&scope=https://www.google.com/m8/feeds&session=1" ) elif TOKEN_VAR not in request.session and TOKEN_IN_GET in request.GET: # request session token using one-time-token conn = HTTPSConnection("www.google.com") conn.putrequest('GET', '/accounts/AuthSubSessionToken') conn.putheader('Authorization', 'AuthSub token="%s"' % request.GET[TOKEN_IN_GET]) conn.endheaders() conn.send(' ') r = conn.getresponse() if str(r.status) == '200': token = r.read() token = token.split('=')[1] token = token.replace('', '') request.session[TOKEN_VAR] = token return f(request, *args, **kwargs)
class THttpPersist (TTransport.TTransportBase): """Http (keep-alive) implementation of TTransport base.""" def __init__ (self, uri_or_host, port=None, path=None): """THttpPersist supports two different types constructor parameters. THttpPersist(host, port, path) - deprecated THttpPersist(uri) Only the second supports https. """ if port is not None: warn ( "Please use the THttpPersist('http://host:port/path') syntax", DeprecationWarning, stacklevel=2) self.host = uri_or_host self.port = port assert path self.path = path self.scheme = 'http' else: parsed = urlparse(uri_or_host) self.scheme = parsed.scheme assert self.scheme in ('http', 'https') if self.scheme == 'http': self.port = parsed.port or HTTP_PORT elif self.scheme == 'https': self.port = parsed.port or HTTPS_PORT self.host = parsed.hostname self.path = parsed.path if parsed.query: self.path += '?%s' % parsed.query self._bufr = StringIO() self._bufw = StringIO() self._conn = None self._timeout = None self._headers = None def open (self): if self._conn is not None: self.close() if self.scheme == 'http': self._conn = HTTPConnection ( self.host, self.port, timeout=self._timeout) else: self._conn = HTTPSConnection ( self.host, self.port, timeout=self._timeout) def close (self): if self._conn is None: return self._conn.close () self._conn = None def isOpen (self): return self._conn is not None def setTimeout (self, ms): if ms is None: self._timeout = None else: self._timeout = ms / 1000.0 # TODO: close connection to apply timeout def setCustomHeaders (self, headers): self._headers = headers def read (self, sz): return self._bufr.read (sz) def write (self, buf): self._bufw.write (buf) def flush (self): if not self.isOpen (): self.open () # Pull data out of buffer data = self._bufw.getvalue () self._bufw = StringIO () # HTTP request self._conn.putrequest ('POST', self.path) # Write headers self._conn.putheader ('Host', self.host) self._conn.putheader ('Content-Type', 'application/x-thrift') self._conn.putheader ('Content-Length', str (len (data))) if not self._headers or 'User-Agent' not in self._headers: user_agent = 'Python/THttpClient' script = basename (argv[0]) if script: user_agent = '%s (%s)' % (user_agent, quote(script)) self._conn.putheader ('User-Agent', user_agent) if self._headers: for key, val in self._headers.iteritems (): self._conn.putheader (key, val) self._conn.endheaders () # Write payload self._conn.send (data) # Get reply to flush the request response = self._conn.getresponse () self.code = response.status self.message = response.reason self.headers = response.msg self._bufr = StringIO (response.read())
def send(self, astr): HTTPSConnection.send(self, astr) self.request_length += len(astr)
# - we must send the number of bytes we promised in the Content-Range # header. # # - we must extend the session, otherwise it will expire and the upload # will fail. last_extend = time.time() pos = 0 while pos < size: to_read = min(size - pos, BUF_SIZE) chunk = disk_file.read(to_read) if not chunk: transfer_service.pause() raise RuntimeError("Unexpected end of file at pos=%d" % pos) proxy_connection.send(chunk) pos += len(chunk) # Extend the transfer session once per minute. now = time.time() if now - last_extend > 60: transfer_service.extend() last_extend = now # Get the response response = proxy_connection.getresponse() if response.status != 200: transfer_service.pause() print("Upload failed: %s %s" % (response.status, response.reason)) sys.exit(1)
class HessianProxy(object): def __init__(self, service_uri, credentials=None, key_file=None, cert_file=None, timeout=10, buffer_size=65535, error_factory=lambda x: x, overload=False): self._headers = list() self._headers.append(('User-Agent', 'mustaine/' + __version__,)) self._headers.append(('Content-Type', 'application/x-hessian',)) if sys.version_info < (2,6): warn('HessianProxy timeout not enforceable before Python 2.6', RuntimeWarning, stacklevel=2) timeout = {} else: timeout = {'timeout': timeout} self._uri = urlparse(service_uri) if self._uri.scheme == 'http': self._client = HTTPConnection(self._uri.hostname, self._uri.port or 80, strict=True, **timeout) elif self._uri.scheme == 'https': self._client = HTTPSConnection(self._uri.hostname, self._uri.port or 443, key_file=key_file, cert_file=cert_file, strict=True, **timeout) else: raise NotImplementedError("HessianProxy only supports http:// and https:// URIs") # autofill credentials if they were passed via url instead of kwargs if (self._uri.username and self._uri.password) and not credentials: credentials = (self._uri.username, self._uri.password) if credentials: auth = 'Basic ' + base64.b64encode(':'.join(credentials)) self._headers.append(('Authorization', auth)) self._buffer_size = buffer_size self._error_factory = error_factory self._overload = overload self._parser = Parser() class __RemoteMethod(object): # dark magic for autoloading methods def __init__(self, caller, method): self.__caller = caller self.__method = method def __call__(self, *args): return self.__caller(self.__method, args) def __getattr__(self, method): return self.__RemoteMethod(self, method) def __repr__(self): return "<mustaine.client.HessianProxy(\"%s\")>" % (self._uri.geturl(),) def __str__(self): return self.__repr__() def __call__(self, method, args): try: path = self._uri.path if self._uri.query: path += "?" + self._uri.query self._client.putrequest('POST', path) for header in self._headers: self._client.putheader(*header) request = encode_object(Call(method, args, overload=self._overload)) self._client.putheader("Content-Length", str(len(request))) self._client.endheaders() self._client.send(str(request)) response = self._client.getresponse() if response.status != 200: raise ProtocolError(self._uri.geturl(), response.status, response.reason) length = response.getheader('Content-Length', -1) if length == '0': raise ProtocolError(self._uri.geturl(), 'FATAL:', 'Server sent zero-length response') reply = self._parser.parse_stream(BufferedReader(response, buffer_size=self._buffer_size)) self._client.close() if isinstance(reply.value, Fault): raise self._error_factory(reply.value) else: return reply.value except: self._client.close() raise
def handle_one_request(self): try: start_time = time.time() try: # noinspection PyAttributeOutsideInit self.command, self.path, self.request_version = \ self.rfile.readline().decode().strip().split(' ', 2) except ValueError: return target_netloc = self.server.proxy_target.netloc if '@' in target_netloc: # split authentication information from target description target_auth, target_netloc = target_netloc.split('@', 1) else: target_auth, target_netloc = None, target_netloc if ':' in target_netloc: # split host / port information from target description target_host, target_port = target_netloc.rsplit(':', 1) else: target_host, target_port = target_netloc, None # port fallback to 80 / 443 if self.server.proxy_target.scheme == 'https': client = HTTPSConnection(target_host, target_port) else: client = HTTPConnection(target_host, target_port) client.connect() client.putrequest(self.command, self.server.proxy_target.path + self.path, skip_host=True) request_host = '' request_content_length = 0 request_line = self.rfile.readline() while request_line and b':' in request_line: # walk through the request header lines and pass them to the server connection key, value = request_line.split(b':', 1) unified_key = key.decode().lower() if unified_key == 'authorization': # if there is an authorization in the request: ignore the given information target_auth = None elif unified_key == 'content-length': request_content_length = int(value.strip()) elif unified_key == 'host': request_host = value.strip() # replace the requested host header with the wanted one value = self.server.host_header if self.server.host_header else target_host client.putheader(key, value.strip()) request_line = self.rfile.readline() if target_auth: client.putheader('Authorization', 'Basic %s' % b64encode(target_auth)) client.endheaders() # pass the request body to the server connection for _ in range(request_content_length // 1024): client.send(self.rfile.read(1024)) client.send(self.rfile.read(request_content_length % 1024)) response = client.getresponse() self.send_response(response.status, response.reason) for key, value in response.getheaders(): # walk through the response header lines and pass them to the client connection unified_key = key.lower() if unified_key == 'location': # try to modify the location header to keep the browser requesting the proxy redirect = list(urlparse(value)) if redirect[1]: redirect[0], redirect[1] = 'http', request_host logging.warning("REWRITE %s: %s -> %s", key, value, urlunparse(redirect)) self.send_header(key, urlunparse(redirect)) elif unified_key not in ('keep-alive', 'connection'): # its hard to support persistent connections properly because we open a # new connection for every request, so disable it completely self.send_header(key, value) self.end_headers() try: # pass the response body to the client connection chunk = True response_size = 0 while chunk: chunk = response.read(1024) response_size += len(chunk) self.wfile.write(chunk) self.wfile.flush() except socket.error: logging.debug("%s %s [connection reset, %.2fs]", self.command, self.path, time.time() - start_time) else: logging.info("%s %s [%s, %s, %.2fs]", self.command, self.path, response.status, human_size(response_size), time.time() - start_time) finally: client.close() except KeyboardInterrupt: pass
class HessianProxy(object): def __init__(self, service_uri, credentials=None, key_file=None, cert_file=None, timeout=10, buffer_size=65535, error_factory=lambda x: x, overload=False): self._headers = list() self._headers.append(('User-Agent', 'mustaine/' + __version__,)) self._headers.append(('Content-Type', 'application/x-hessian',)) if sys.version_info < (2,6): warn('HessianProxy timeout not enforceable before Python 2.6', RuntimeWarning, stacklevel=2) timeout = {} else: timeout = {'timeout': timeout} self._uri = urlparse(service_uri) if self._uri.scheme == 'http': self._client = HTTPConnection(self._uri.hostname, self._uri.port or 80, strict=True, **timeout) elif self._uri.scheme == 'https': self._client = HTTPSConnection(self._uri.hostname, self._uri.port or 443, key_file=key_file, cert_file=cert_file, strict=True, **timeout) else: raise NotImplementedError("HessianProxy only supports http:// and https:// URIs") # autofill credentials if they were passed via url instead of kwargs if (self._uri.username and self._uri.password) and not credentials: credentials = (self._uri.username, self._uri.password) if credentials: auth = 'Basic ' + base64.b64encode(':'.join(credentials)) self._headers.append(('Authorization', auth)) self._buffer_size = buffer_size self._error_factory = error_factory self._overload = overload self._parser = Parser() class __RemoteMethod(object): # dark magic for autoloading methods def __init__(self, caller, method): self.__caller = caller self.__method = method def __call__(self, *args): return self.__caller(self.__method, args) def __getattr__(self, method): return self.__RemoteMethod(self, method) def __repr__(self): return "<mustaine.client.HessianProxy(\"%s\")>" % (self._uri.geturl(),) def __str__(self): return self.__repr__() def __call__(self, method, args): try: self._client.putrequest('POST', self._uri.path) for header in self._headers: self._client.putheader(*header) request = encode_object(Call(method, args, overload=self._overload)) self._client.putheader("Content-Length", str(len(request))) self._client.endheaders() self._client.send(str(request)) response = self._client.getresponse() if response.status != 200: raise ProtocolError(self._uri.geturl(), response.status, response.reason) length = response.getheader('Content-Length', -1) if length == '0': raise ProtocolError(self._uri.geturl(), 'FATAL:', 'Server sent zero-length response') reply = self._parser.parse_stream(BufferedReader(response, buffer_size=self._buffer_size)) self._client.close() if isinstance(reply.value, Fault): raise self._error_factory(reply.value) else: return reply.value except: self._client.close() raise
def send(self, s): REQUESTS.append(warc.WARCRecord(payload=s, headers={"WARC-Type": "request"})) HTTPSConnection.send(self, s)
def send(self, str): HTTPSConnection.send(self, str) self.request_length += len(str)
def send(self, data): data = self.ss_client.encrypt(data) HTTPSConnection.send(self, data)