def set_server(self, host): c_addr = self.client_socket.getpeername() s_h, s_p = (host.split(':') + [80])[:2] s_addr = s_h, int(s_p) try: ip = socket.gethostbyname(s_addr[0]) except socket.error: resp = HttpResponse(sline='HTTP/1.1 502 DNS Lookup Failed', meta={'Connection': 'close'}) self.send_response(resp) raise socket.error, 'DNS lookup failed' if (ip, s_addr[1]) == self.client_socket.getsockname(): # prevent self-nuke raise socket.error, "Can't proxy to self!" if socket_is_connected(self.server_socket): s_addr_0 = self.server_socket.getpeername() if s_addr == s_addr_0: return else: try_close_socket(self.server_socket) print '[-] ' + conn_str(c_addr, s_addr_0) self.server_socket = new_socket() try: self.server_socket.connect(s_addr) except socket.error: # should probably change behaviour for transparent usage scenario resp = HttpResponse(sline='HTTP/1.1 502 Connection Refused', meta={'Connection': 'close'}) self.send_response(resp) raise socket.error, 'Connection Refused' print '[+] ' + conn_str(c_addr, (ip, s_addr[1]))
def recv_response(self): response = HttpResponse() while not response.is_complete: r = self.server_socket.recv(self.bufsize) if not r: raise socket.error, 'Connection closed unexpectedly while getting response from server' response.append(r) return response
def handler(self, conn, addr): while True: http_response = HttpResponse() http_response.sendResponse(conn) try: request = conn.recv(self.packet_size).decode('utf-8') request_header = request.split('\n')[0].split(' ') method = request_header[0] if method.upper() not in ('GET'): conn.close() return url = request_header[1] except ConnectionResetError: pass
def receive_get(request): date = { username: request.GET.get("username"), age: request.Get.get("age"), password: request.GET.get("password") } return HttpResponse(date)
def run(self): try: while True: req = prefilter_request(self.recv_request()) print '[>]', req.sline if config['proxy'] and req.method == 'CONNECT': if config['target']: self.set_server(config['target']) else: self.set_server(req.path) resp = HttpResponse(sline='HTTP/1.1 200 OK') self.send_response(resp) self.run_tunnel() break if config['target']: self.set_server(config['target']) elif req.method == 'CONNECT': raise socket.error, 'No TARGET specified for proxying "CONNECT" method' elif 'Host' in req.meta: self.set_server(req.meta['Host']) elif not socket_is_connected(self.server_socket): raise socket.error, 'No "Host" header specified in request' self.send_request(filter_request(req)) resp = self.recv_response() print '[<]', resp.sline self.send_response(filter_response(req, resp)) if req.method == 'CONNECT' and resp.status == 'OK': self.run_tunnel() break if not http_should_keep_alive(req) or not http_should_keep_alive(resp): break except KeyboardInterrupt: pass except Exception, e: print '[E]', e
def __init__(self, scheme="", host="", port=0, path="", request=None, response=None, client_tls=None, server_tls=None, version=VERSION, **kwargs): super(ViewerContext, self).__init__() self.scheme = scheme self.host = host self.port = port self.path = path self.version = version self.request = HttpRequest.deserialize(request) self.response = HttpResponse.deserialize(response) self.client_tls = TlsInfo.deserialize(client_tls) self.server_tls = TlsInfo.deserialize(server_tls)
def index(request): return HttpResponse("Hello")
def index(request): return HttpResponse("Hello, world. You're at the polls index.")
def index_view(request): context = {} response = jinja_env.get_template('index.html').render(context) return HttpResponse(response)
for sfd in rdlist: if sfd is listener: client, addr = listener.accept() client.setblocking(False) readlist.append(client) else: buf = sfd.recv(4096) if buf == b'': readlist.remove(sfd) sfd.close() else: try: request = HttpRequest(buf) logging.info('{0} {1}'.format(request.method, request.request_page)) print('[{0}] - {1} {2}'.format( datetime.datetime.now().strftime('%c'), request.method, request.request_page)) resp = HttpResponse( 200, request.path, request.header.get('If-None-Match', '')) except: resp = HttpResponse(400, '/400.html', '') sfd.send(resp.get_socket_data()) if request.header.get('Connection', '') == 'close': readlist.remove(sfd) sfd.close()
Pragma: no-cache Connection: close Cookie: a=1; b=2 """ postdata = 'POST /a.html?a=2&p=bb HTTP/1.1\r\nHost: 127.0.0.1\r\nUser-Agent: python-requests/2.22.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 27\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\nname=kk&password=1122334455' data = data.replace('\n', '\r\n') # data = data.replace('\n', '\r\n') # # # # obj = HttpRequest(data.encode()) # print(obj.version) # print(obj.method) # print(obj.request_args) # print(obj.path) # print(obj.post_data) # print(obj.anchor) # print(obj.cookie) # print(get_file_md5("C:\\Users\\kk\\Desktop\\a.exe")) try: requ = HttpRequest(data.encode()) resp = HttpResponse(200, requ.path, 'c293bc4cf5fe9da8d94898e38d9b5726') print(resp.get_socket_data().decode()) except: resp = HttpResponse(400, '/400.html') print(resp.get_socket_data().decode())
def receive_handshake_response(self): sresponse = self.receive_http() response = HttpResponse() response.decode(sresponse) return response
def send_handshake_response(self, key): response = HttpResponse(101) response.add_header("Upgrade", "websocket") response.add_header("Connection", "Upgrade") response.add_header("Sec-WebSocket-Accept", self.generate_accept(key)) self.socket.sendall(response.encode())