2

Hello Is there any way to connect through a proxy using sockets in python. This is giving me an error

import socket, sys
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("www.python.org", 80))

Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
soc.connect(("http://www.python.org",80))
File "<string>", line 1, in connect
gaierror: [Errno -5] No address associated with hostname

Thanks

Manoj I
  • 1,089
  • 1
  • 13
  • 18

2 Answers2

1

You can try using SocksiPy: it'll establish a connection to your proxy server and do all that work for you.

Mew
  • 1,049
  • 7
  • 17
0

I was able to solve this issue using urllib2 like so:

import urllib2 

opener = urllib2.build_opener(
     urllib2.ProxyHandler({"http":"proxy_ip_address:port_number";}),
     urllib2.ProxyHandler({"https":"proxy_ip_address:port_number";}),
)
urllib2.install_opener(opener) 

for line in urllib2.urlopen("py4inf.com/code/romeo.txt"): 
    print line.strip() 
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
Jack Vo
  • 55
  • 1
  • 2
  • 6