Having trouble setting up an extremely simple server...

Roy Smith roy at panix.com
Thu Nov 21 21:33:13 EST 2013


In article <9e773107-5a6c-486b-bef2-186101d8f141 at googlegroups.com>,
 cilantromc at gmail.com wrote:

> I'm attempting to set up an extremely simple server that receives a string, 
> and returns a string. However, I have 2 problems. I'm able to receive the 
> string from the client fine, but it only will receive it once. After I send 
> another string from the client, it doesn't come up on the server... Also, I 
> want to send something BACK to the client-side, but I can't seem to see 
> how... Please help! I'm very new to networking, but I've been using Python 
> for a while now, just recent;y getting into networking, trying to get things 
> down.
> 
> SERVER.PY:
> 
> import socket;
> serverReady = True;
> 
> serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
> serverSock.bind(('localhost', 8081));
> serverSock.listen(10);
> 
> while (True):
>     connection, address = serverSock.accept();
>     if (serverReady):
>         serverSockBuffer = connection.recv(1024);
>         if (len(serverSockBuffer) > 0):
>             print serverSockBuffer;
>     if (raw_input("Ready?: ") in ['yes', 'y']):
>         serverReady = True;
>     else:
>         serverReady = False;

First thing, get rid of all those semicolons.  This is Python you're 
writing, not C++.  Likewise, the extra parens in your while statements.

Your problem is that you're doing the accept() inside your main loop on 
the server.  You want to be doing it once, outside the loop.



More information about the Python-list mailing list