5

i am trying to connect to TOR through python but it doesnt let me the code is:

def tor_connection():
    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True)
    socket.socket = socks.socksocket

def main():
    tor_connection()
    print('Connected to tor')
    con = httplib.HTTPConnection('myip.dnsomatic.com/')
    con.request('GET', '/')
    response = con.getresponse()
    print(response.read())

main()

even though its giving me the next error message:

Traceback (most recent call last):
  File "C:/Users/anon/PycharmProjects/Scraper/tor.py", line 198, in <module>
    main()
  File "C:/Users/anon/PycharmProjects/Scraper/tor.py", line 194, in main
    con.request('GET', '/')
  File "C:\Python27\lib\httplib.py", line 1001, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 1035, in _send_request
    self.endheaders(body)
  File "C:\Python27\lib\httplib.py", line 997, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 850, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 812, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 793, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed

i am just a beginner could someone help me out please? i have tried it in another laptop but its the same error message

godhck
  • 59
  • 2
  • If you want to draw attention to your questions... please don't post throwaway answers just to bump your question. We have a process, participate enough to earn enough rep to place bounties on your questions you want more attention on. – Jeff Mercado Feb 09 '15 at 04:37

1 Answers1

0

It's not a problem of socks. You need to specify the hostname without the trailing /:

>>> # with /
>>> httplib.HTTPConnection('myip.dnsomatic.com/').request('GET', '/')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
    self.send(msg)
  File "/usr/lib/python2.7/httplib.py", line 791, in send
    self.connect()
  File "/usr/lib/python2.7/httplib.py", line 772, in connect
    self.timeout, self.source_address)
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

>>> # without /
>>> httplib.HTTPConnection('myip.dnsomatic.com').request('GET', '/')
>>> 
falsetru
  • 314,667
  • 49
  • 610
  • 551
  • File "C:\Python27\lib\socket.py", line 571, in create_connection raise err socket.error: [Errno 10061] No connection could be made because the target machine actively refused it – godhck Feb 09 '15 at 04:45
  • @godhck, Check firewall settings. – falsetru Feb 09 '15 at 05:08
  • i am a newby could you tell me how? – godhck Feb 09 '15 at 05:09
  • @godhck, It seems like you're using windows. Right? Search for `windows firewall` in search engine. – falsetru Feb 09 '15 at 05:13
  • yes i am running windows even though i dont know how to fix it – godhck Feb 09 '15 at 06:23
  • @godhck, Try this to turn the firewall off: http://windows.microsoft.com/en-us/windows/turn-windows-firewall-on-off#turn-windows-firewall-on-off=windows-7. Restore it once you finished, and figure out how to properly allow TOR port accept connection. – falsetru Feb 09 '15 at 06:26
  • i have turn my firewall off and it still say the same message socket.error: [Errno 10061] No connection could be made because the target machine actively refused it – godhck Feb 09 '15 at 06:43
  • def tor_connection(): socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050, True) socket.socket = socks.socksocket def main(): tor_connection() print('Connected to tor') con = httplib.HTTPConnection('whatismyip.com').request('GET', '/') response = con.getresponse() print(response.read()) main() – godhck Feb 09 '15 at 06:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70541/discussion-between-godhck-and-falsetru). – godhck Feb 09 '15 at 06:56
  • @godhck, How about post a separated question with your code and new exception. – falsetru Feb 09 '15 at 06:57
  • @godhck, I saw your new question post. Why don't you post the fixed version? (without the trailing `/`) – falsetru Feb 09 '15 at 09:28