3

I'm trying to use sockets with python, but I keep on getting this error message:

import socket
>>> s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
>>> s.connect(('localhost', 8000))
Traceback (most recent call last):
  File "stdin", line 1, in module
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

config in /etc/hosts is :

::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

System is ubuntu 12.04 , no firewall configured. Tried to change port number , but no luck; it works only with domains different from "localhost". Does someone knows why it happens?

Mat
  • 202,337
  • 40
  • 393
  • 406
Michele
  • 41
  • 1
  • 2
  • 1
    Is anything listening on that port on your machine? – Mat May 05 '12 at 16:57
  • No, nothing. Could that be a firewall issue or a privileges one? .I had the same problem on win7 , so i configured the win firewall to allow python to connect to localhost , and then it worked .. – Michele May 05 '12 at 18:30
  • 1
    If nothing is listening, you can't connect. – Mat May 05 '12 at 18:31

1 Answers1

2

try

s = socket.socket( socket.AF_INET6, socket.SOCK_STREAM )
s.connect(('::1', 8000))

you seem to have only ipv6 enabled.

User
  • 14,131
  • 2
  • 40
  • 59
  • sorry.. after a double check my /etc/hosts is like this 127.0.0.1 localhost 127.0.1.1 michele-System-Product-Name # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters – Michele May 05 '12 at 20:11