Wednesday, May 28, 2008

Proxy|Python code accross proxy

Python code accross proxy
=====================================
#!/usr/bin/env python
"""
Test read a htm from internet and across intranet proxy

"""

import sys
import getopt
import urllib2

def startTask():
proxy={
'user':'Stephen',
'pass':'pass',
'host':'proxy.com',
'port':8080
}

proxy_support = urllib2.ProxyHandler({"http":"http://%(user)s:%(pass)s@%(host)s:%(port)d" % proxy})
#proxy_support = urllib2.ProxyHandler({"http":"http://username:password@proxy.com:8080"})


opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)

# install it
urllib2.install_opener(opener)

sock = urllib2.urlopen('http://www.python.org/')
print sock.headers
print sock.read()

if __name__ == "__main__":
startTask()

No comments: