[Tutor] Socket module woes.

Fred Allen fallen@leveltwo.com
Wed Jun 4 14:01:15 2003


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------ =_NextPart_001_01C32AC3.C1035EDE
Content-Type: text/plain

Dear Sirs and Mesdames:

I'm attempting to familiarize myself with Python's low-level socket
module.  The module's socket class seems to me the quintessence of
clarity.  Nonetheless, I've been unable to make the simple "Echo Server
Program," appearing in section 7.2.2 of the Python Library Reference,
work. 

I've replicated it as carefully as possible (for me, in early dotage).
Still, when I initiate its execution, it "hangs."  Upon trying to close
my development application (PythonWin 2.2.2, # 27, 11/26/02 on Windows
2000 Pro), W2K presents a dialog box informing me that the program is
"not responding."   I'm surely doing something causative in ignorance.
The code appears below:

# Echo server program
from socket import *
HOST = ''                       # Symbolic name meaning the local host
PORT = 50007                    # Arbitrary non-privileged server
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))  # Altered to provide "socket.bind" method's
expected tuple--FA.
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.send(data)
conn.close()

I've identified the offending statement.  By interactively entering each
statement in sequence, as follows, the system hangs when I <Enter> the
"accept" method call.  I understand, of course, that the accept method
is intended to suspend its program's activity whilst awaiting a
connection attempt.  But, the way I've done it, it suspends not only its
own program, but the entire development environment, so I cannot
initiate the client's execution. 

    >>> from socket import *
    >>> HOST = ''
    >>> PORT = 50007
    >>> s = socket(AF_INET,SOCK_STREAM)
    >>> s.bind((HOST, PORT))
    >>> s.listen(1)
    >>> conn, addr = s.accept()

I would not trouble you, had I spent fewer hours trying everything
sensible, and some things not so.  I would be most grateful for any
light that any among you might shed upon my problem.  With thanks in
advance, I am,

Gratefully,

Fred Allen


------ =_NextPart_001_01C32AC3.C1035EDE
Content-Type: text/html
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3DUS-ASCII">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.1960.3">
<TITLE>Socket module woes.</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>Dear Sirs and Mesdames:</FONT>
</P>

<P><FONT SIZE=3D2>I'm attempting to familiarize myself with Python's =
low-level socket</FONT>
<BR><FONT SIZE=3D2>module.&nbsp; The module's socket class seems to me =
the quintessence of</FONT>
<BR><FONT SIZE=3D2>clarity.&nbsp; Nonetheless, I've been unable to make =
the simple &quot;Echo Server</FONT>
<BR><FONT SIZE=3D2>Program,&quot; appearing in section 7.2.2 of the =
Python Library Reference,</FONT>
<BR><FONT SIZE=3D2>work. </FONT>
</P>

<P><FONT SIZE=3D2>I've replicated it as carefully as possible (for me, =
in early dotage).</FONT>
<BR><FONT SIZE=3D2>Still, when I initiate its execution, it =
&quot;hangs.&quot;&nbsp; Upon trying to close</FONT>
<BR><FONT SIZE=3D2>my development application (PythonWin 2.2.2, # 27, =
11/26/02 on Windows</FONT>
<BR><FONT SIZE=3D2>2000 Pro), W2K presents a dialog box informing me =
that the program is</FONT>
<BR><FONT SIZE=3D2>&quot;not responding.&quot;&nbsp;&nbsp; I'm surely =
doing something causative in ignorance.</FONT>
<BR><FONT SIZE=3D2>The code appears below:</FONT>
</P>

<P><FONT SIZE=3D2># Echo server program</FONT>
<BR><FONT SIZE=3D2>from socket import *</FONT>
<BR><FONT SIZE=3D2>HOST =3D =
''&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # =
Symbolic name meaning the local host</FONT>
<BR><FONT SIZE=3D2>PORT =3D =
50007&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Arbitrary =
non-privileged server</FONT>
<BR><FONT SIZE=3D2>s =3D socket(AF_INET, SOCK_STREAM)</FONT>
<BR><FONT SIZE=3D2>s.bind((HOST, PORT))&nbsp; # Altered to provide =
&quot;socket.bind&quot; method's expected tuple--FA.</FONT>
<BR><FONT SIZE=3D2>s.listen(1)</FONT>
<BR><FONT SIZE=3D2>conn, addr =3D s.accept()</FONT>
<BR><FONT SIZE=3D2>print 'Connected by', addr</FONT>
<BR><FONT SIZE=3D2>while 1:</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; data =3D conn.recv(1024)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; if not data: break</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; conn.send(data)</FONT>
<BR><FONT SIZE=3D2>conn.close()</FONT>
</P>

<P><FONT SIZE=3D2>I've identified the offending statement.&nbsp; By =
interactively entering each</FONT>
<BR><FONT SIZE=3D2>statement in sequence, as follows, the system hangs =
when I &lt;Enter&gt; the</FONT>
<BR><FONT SIZE=3D2>&quot;accept&quot; method call.&nbsp; I understand, =
of course, that the accept method</FONT>
<BR><FONT SIZE=3D2>is intended to suspend its program's activity whilst =
awaiting a</FONT>
<BR><FONT SIZE=3D2>connection attempt.&nbsp; But, the way I've done it, =
it suspends not only its</FONT>
<BR><FONT SIZE=3D2>own program, but the entire development environment, =
so I cannot</FONT>
<BR><FONT SIZE=3D2>initiate the client's execution. </FONT>
</P>

<P><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; from socket import =
*</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; HOST =3D ''</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; PORT =3D =
50007</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; s =3D =
socket(AF_INET,SOCK_STREAM)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; s.bind((HOST, =
PORT))</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; s.listen(1)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; &gt;&gt;&gt; conn, addr =3D =
s.accept()</FONT>
</P>

<P><FONT SIZE=3D2>I would not trouble you, had I spent fewer hours =
trying everything</FONT>
<BR><FONT SIZE=3D2>sensible, and some things not so.&nbsp; I would be =
most grateful for any</FONT>
<BR><FONT SIZE=3D2>light that any among you might shed upon my =
problem.&nbsp; With thanks in</FONT>
<BR><FONT SIZE=3D2>advance, I am,</FONT>
</P>

<P><FONT SIZE=3D2>Gratefully,</FONT>
</P>

<P><FONT SIZE=3D2>Fred Allen</FONT>
</P>

</BODY>
</HTML>
------ =_NextPart_001_01C32AC3.C1035EDE--