server question

Peter Hansen peter at engcorp.com
Sat Aug 11 00:16:50 EDT 2001


vin wrote:
> 
> import socket
> 
> def start_server(PORT):
>     HOST = ' '                 # Symbolic name meaning the local host
>     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>     s.bind((HOST, PORT))
>     s.listen(1)
>     conn, addr = s.accept()
>     print 'Connected by', addr
>     while 1:
>         data = conn.recv(1024)
>         if not data : break
>         conn.send(eval(data))
>     conn.close()
> 
> When I send "5*6" the server chrashes.  ....

How are you "sending" this?  You can't type it fast enough
in telnet, I think, to avoid having the first character
sent (and evaluated) before you manage to type the second
character, so have you got some client program which
you've also written?  Are you certain it is really sending
what you think it is?  

(Put another way: the above code won't work anyway.  
Sockets provide no guarantee that all the data sent 
will be received by recv() in a single call, so you 
might end up with "5*" in the first pass, and "6" in 
the second.)


> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "server.py", line 13, in start_server
>     print 'Evaluated', eval(data)
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This line doesn't exist in the snippet you've posted.  
Please post the code you are actually running with
the error it generates, so we don't have to guess.

(Or try this one: it looks like you are trying to 
duplicate the functionality of the monitor_server
included in Medusa (http://www.nightmare.com/ ).
Maybe you can just steal from there.  And if you 
planned to extend the above server to do
more than simple one-line expressions, you 
might also want to look at the library modules
code.py and codeop.py.)

-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list