forked from mdmonk/python_snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
https_with_proxy_auth.py
31 lines (26 loc) · 989 Bytes
/
https_with_proxy_auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import httplib,base64,socket
user='proxy_login';passwd='proxy_pass'
host='login.yahoo.com';port=443
phost='proxy_host';pport=80
#setup basic authentication
user_pass=base64.encodestring(user+':'+passwd)
proxy_authorization='Proxy-authorization: Basic '+user_pass+'\r\n'
proxy_connect='CONNECT %s:%s HTTP/1.0\r\n'%(host,port)
user_agent='User-Agent: python\r\n'
proxy_pieces=proxy_connect+proxy_authorization+user_agent+'\r\n'
#now connect, very simple recv and error checking
proxy=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
proxy.connect((phost,pport))
proxy.sendall(proxy_pieces)
response=proxy.recv(8192)
status=response.split()[1]
if status!=str(200): raise 'Error status=',str(status)
#trivial setup for ssl socket
ssl = socket.ssl(proxy, None, None)
sock = httplib.FakeSocket(proxy, ssl)
#initalize httplib and replace with your socket
h=httplib.HTTPConnection('localhost')
h.sock=sock
h.request('GET','/')
r=h.getresponse()
print r.read()