[Tutor] Socket Programming HOWTO doesn't work!
Jeff Shannon
jeff@ccvcorp.com
Mon Jun 23 17:41:01 2003
Patrick Kirk wrote:
> There is a Python socket programming tutorial shipped with PythonWin,
> it always comes up on Google and seems to be considered the reference
> for Python.
>
> So I tried it.
>
> It bombs out on Windows and Linux. I tried adding from socket import
> * to the top but that failed as well.
You need to 'import socket' instead of 'from socket import *'. Notice
that all the socket features are accessed using 'socket.XXXXX' -- this
means 'look in the object named socket for the object named XXXXX'. You
need to have that 'socket' module object for that to work. 'import
socket' binds the module to the name socket; 'from socket import *'
loads the module, and puts all of the names *within* the module into the
current namespace, but it doesn't bind the name 'socket' to the module
object. (In fact, it binds the name 'socket' to a particular class
within that module, which is referred to in the example as
'socket.socket'. The interpreter then tries to find various module
attributes within that class, and gives an error when it can't find them.)
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.
Jeff Shannon
Technician/Programmer
Credit International