[Tutor] Socket Programming HOWTO doesn't work!

Jeff Shannon jeff@ccvcorp.com
Mon Jun 23 18:29:02 2003


Patrick Kirk wrote:

>
>
> Jeff Shannon wrote:
> [...]
>
>>
>> Keep in mind, also, that when you run this script, that process will 
>> be tied up listening on the socket.  This means that if you run it 
>> from within PythonWin or IDLE, the interpreter will appear to be 
>> "locked up" -- in fact, it's just waiting for something to come in on 
>> the socket. You may want to open a separate command (DOS) window to 
>> run this in, so that you'll still be able to access the IDE while 
>> it's running.
>
> [...]
>
> Still doesn't work.
>
> import socket
>
> #create an INET, STREAMing socket
> serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> #bind the socket to a public host,
> # and a well-known port
> serversocket.bind((socket.gethostname(), 8888))
> #become a server socket
> serversocket.listen(5)
>
> When I try it on windows I get this:
>
> Traceback (most recent call last):
>   File 
> "C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", 
> line 301, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Documents and Settings\patrick\Desktop\p2pdev\simple.py", 
> line 1, in ?
>     import socket
>   File "socket.py", line 72, in ?
>     errorTab[10004] = "The operation was interrupted."
>   File "C:\Python22\lib\SocketServer.py", line 273, in ?
>     class TCPServer(BaseServer):
>   File "C:\Python22\lib\SocketServer.py", line 316, in TCPServer
>     address_family = socket.AF_INET
> AttributeError: 'module' object has no attribute 'AF_INET'


Any possibility that you have some other socket.py file that's getting 
picked up instead?  Try this and see if you get the same result that I do:

 >>> import socket
 >>> print socket.__file__
C:\Python22\lib\socket.py
 >>>

Also, try executing the script from a DOS window, instead of from inside 
PythonWin.  Because PythonWin runs code in the same process that 
PythonWin runs in, it's possible that there's some sort of namespace 
contention going on.

Jeff Shannon
Technician/Programmer
Credit International