Python socket module. Connecting to an HTTP proxy then performing a GET request on an external resource -
to begin with, understand there other modules such requests better suited , simpler use, want use socket module better understand http.
i have simple script following:
client ---> http proxy ---> external resource (get google.com)
i able connect http proxy alright, when send request headers google.com proxy, doesn't serve me response @ all.
#!/usr/bin/python import socket import sys headers = """get / http/1.1\r\n host: google.com\r\n\r\n""" socket = socket host = "165.139.179.225" #proxy server ip port = 8080 #proxy server port try: s = socket.socket() s.connect((host,port)) s.send(("connect {0}:{1} http/1.1\r\n" + "host: {2}: {3}\r\n\r\n").format(socket.gethostbyname(socket.gethostname()),1000,port,host)) print s.recv(1096) s.send(headers) response = s.recv(1096) print response s.close() except socket.error,m: print str(m) s.close() sys.exit(1)
to make http request proxy open connection proxy server , send http-proxy request. request same normal http request, contains absolute url instead of relative url, e.g.
> http://www.google.com http/1.1 > host: www.google.com > ... < http response
to make https request open tunnel using connect method , proceed inside tunnel normally, ssl handshake , normal non-proxy request inside tunnel, e.g.
> connect www.google.com:443 http/1.1 > < .. read response connect request, must 200 ... .. establish tls connection inside tunnel > / http/1.1 > host: www.google.com
Comments
Post a Comment