Having trouble setting up an extremely simple server...

Ned Batchelder ned at nedbatchelder.com
Thu Nov 21 21:58:04 EST 2013


On Thursday, November 21, 2013 9:36:32 PM UTC-5, Cilantro MC wrote:
> On Thursday, November 21, 2013 9:33:13 PM UTC-5, Roy Smith wrote:
> > 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.
> 
> I prefer using the semicolons... They aren't making my code wrong... I use other programming languages from time to time, and I'd rather just always use semicolons, as with the parentheses. 

Well then, why not use two semicolons at the end of each statement? Or even three?  If your answer is, "Because that looks silly and is unnecessary," then now you know how Python programmers feel about one semicolon! :)

--Ned.



More information about the Python-list mailing list