python利用socket或者httplib模块代理访问网络

httplib代理方法

conn = httplib.HTTPConnection('120.28.64.69',8080)
conn.request('GET',r'http://www.ip.cn/getip.php?action=getip')
r = conn.getresponse()
while 1:
data = r.read(1024)
print data
   if len(data)<1024:
    break  

socket代理方法
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('120.28.64.69', 8080))
s.send(r'GET http://www.ip.cn/getip.php?action=getip HTTP/1.1\r\nAccept: text/plain\r\n\r\n')
data1 = ''
while 1:
data = s.recv(1024)
print data
if len(data) < 1024:
   break
s.close()

self.response.headers['content-type'] = 'text/html'
self.response.out.write(data1)

url='http://www.google.com'
urllib.FancyURLopener(proxies={'http':'http://120.28.64.69:8080'}).open(url).read()

你可能感兴趣的:(python利用socket或者httplib模块代理访问网络)