Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60187 > unrolled thread
| Started by | cilantromc@gmail.com |
|---|---|
| First post | 2013-11-21 17:56 -0800 |
| Last post | 2013-11-23 01:16 +1100 |
| Articles | 11 — 8 participants |
Back to article view | Back to comp.lang.python
Having trouble setting up an extremely simple server... cilantromc@gmail.com - 2013-11-21 17:56 -0800
Re: Having trouble setting up an extremely simple server... Roy Smith <roy@panix.com> - 2013-11-21 21:33 -0500
Re: Having trouble setting up an extremely simple server... Cilantro MC <cilantromc@gmail.com> - 2013-11-21 18:36 -0800
Re: Having trouble setting up an extremely simple server... Roy Smith <roy@panix.com> - 2013-11-21 21:50 -0500
Re: Having trouble setting up an extremely simple server... Ned Batchelder <ned@nedbatchelder.com> - 2013-11-21 18:58 -0800
Re: Having trouble setting up an extremely simple server... Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-22 17:02 +1300
Re: Having trouble setting up an extremely simple server... Chris Angelico <rosuav@gmail.com> - 2013-11-22 15:30 +1100
Re: Having trouble setting up an extremely simple server... Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-22 05:32 +0000
Re: Having trouble setting up an extremely simple server... rusi <rustompmody@gmail.com> - 2013-11-21 21:45 -0800
Re: Having trouble setting up an extremely simple server... Roy Smith <roy@panix.com> - 2013-11-22 08:41 -0500
Re: Having trouble setting up an extremely simple server... Chris Angelico <rosuav@gmail.com> - 2013-11-23 01:16 +1100
| From | cilantromc@gmail.com |
|---|---|
| Date | 2013-11-21 17:56 -0800 |
| Subject | Having trouble setting up an extremely simple server... |
| Message-ID | <9e773107-5a6c-486b-bef2-186101d8f141@googlegroups.com> |
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;
CLIENT.PY:
import socket;
clientSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
clientSock.connect(('localhost', 8081));
user = raw_input("Username: ");
while (True):
sendData = raw_input("Send: ");
clientSock.send(str(sendData + ' -- ' + user));
print clientSock;
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-21 21:33 -0500 |
| Message-ID | <roy-F7D4A2.21331321112013@news.panix.com> |
| In reply to | #60187 |
In article <9e773107-5a6c-486b-bef2-186101d8f141@googlegroups.com>,
cilantromc@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.
[toc] | [prev] | [next] | [standalone]
| From | Cilantro MC <cilantromc@gmail.com> |
|---|---|
| Date | 2013-11-21 18:36 -0800 |
| Message-ID | <ac9aae1d-b86c-4ea0-b41f-a044e053a932@googlegroups.com> |
| In reply to | #60191 |
On Thursday, November 21, 2013 9:33:13 PM UTC-5, Roy Smith wrote:
> In article <9e773107-5a6c-486b-bef2-186101d8f141@googlegroups.com>,
>
> cilantromc@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. I will try that though, moving the accept(). What about sending information back to the client?
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-21 21:50 -0500 |
| Message-ID | <roy-BCFB63.21501321112013@news.panix.com> |
| In reply to | #60192 |
In article <ac9aae1d-b86c-4ea0-b41f-a044e053a932@googlegroups.com>, Cilantro MC <cilantromc@gmail.com> wrote: > > 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 suggest you find another way to post other than Google Groups. As you can see from what I've quoted above, their interface ends up double-spacing everything and making it difficult to read. > 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, no, they're not making your code wrong, but it's just not the way people write Python. >What about sending information back to the client? You can use the same socket for communication in both directions. The client and server need to alternate doing send() and recv() calls. Client does send() and server does recv(). Then, the server does send() and the client does recv().
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2013-11-21 18:58 -0800 |
| Message-ID | <e398c97e-3489-4e27-8dd8-a36381655fe4@googlegroups.com> |
| In reply to | #60192 |
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@googlegroups.com>,
> > cilantromc@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.
[toc] | [prev] | [next] | [standalone]
| From | Gregory Ewing <greg.ewing@canterbury.ac.nz> |
|---|---|
| Date | 2013-11-22 17:02 +1300 |
| Message-ID | <bf839pFtt6qU1@mid.individual.net> |
| In reply to | #60192 |
Cilantro MC wrote: > 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. It's your choice, but just be aware that other Python programmers reading your code will find it annoying, and it's not generally a good idea to annoy people that you're asking for help. :-) -- Greg
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-22 15:30 +1100 |
| Message-ID | <mailman.3027.1385094624.18130.python-list@python.org> |
| In reply to | #60197 |
On Fri, Nov 22, 2013 at 3:02 PM, Gregory Ewing <greg.ewing@canterbury.ac.nz> wrote: > Cilantro MC wrote: >> >> 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. > > > It's your choice, but just be aware that other Python > programmers reading your code will find it annoying, > and it's not generally a good idea to annoy people > that you're asking for help. :-) Or, worse, to confuse us as to the purpose of your code. People here are used to spotting bugs, and that bug-spotting is hugely aided by certain conventions - many of which are laid out in PEP 8 [1], though of course you don't need to follow all of those guidelines. I'm a polyglot programmer, myself, so I completely understand your desire to put semicolons around the place. Actually, my work in C/C++ and Pike, where semicolons are mandatory, isn't why I want to put them in; no, it's because of JavaScript, where they're technically optional but the omission can sometimes lead to misparsing of code - THAT is where I'll be careful to put them in. Omit a semi in C? Simple compilation error, almost certainly. Omit one in JS? Works perfectly... or at least seems to. Omit them all in Python? Actually genuinely works perfectly. :) ChrisA [1] http://www.python.org/dev/peps/pep-0008/
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-22 05:32 +0000 |
| Message-ID | <528eec7a$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #60192 |
On Thu, 21 Nov 2013 18:36:32 -0800, Cilantro MC wrote:
> 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.
There are all sorts of things that you can do that don't make your code
"wrong" but do make it difficult to deal with. Why stop with semi-colons?
import socket; pass; pass; pass; pass; pass;
serverReady = ((True is True) is True) is True) is True);
serverSock = socket . \
socket(
socket . \
AF_INET \
, \
socket . \
SOCK_STREAM \
) \
;
is legal, correct code and works fine. So why not write that?
Because it is *annoying* code. It doesn't read fluently. The unnecessary
punctuation and silly spacing makes it harder to read.
To a fluent Python programmer, that's what semi-colons are like, although
to a lesser degree. An unnecessary distraction and annoyance, rather like
people who talk like this:
"Er, I prefer, um, using the semicolons, um... They, um, aren't making
my, um, code wrong... er, I use other, um, programming languages, um,
from time to time, um, and I'd, er, rather, um, just always, um, use
semicolons, um, as with, er, the parentheses, um."
Pretty horrible. The sentences are still grammatically correct. But that
doesn't mean it's a good idea to talk like that.
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2013-11-21 21:45 -0800 |
| Message-ID | <de88d692-4c52-4ff6-bdae-d883c3de12bc@googlegroups.com> |
| In reply to | #60203 |
On Friday, November 22, 2013 11:02:43 AM UTC+5:30, Steven D'Aprano wrote: > To a fluent Python programmer, that's what semi-colons are like, although > to a lesser degree. An unnecessary distraction and annoyance, rather like > people who talk like this: > "Er, I prefer, um, using the semicolons, um... They, um, aren't making > my, um, code wrong... er, I use other, um, programming languages, um, > from time to time, um, and I'd, er, rather, um, just always, um, use > semicolons, um, as with, er, the parentheses, um." > Pretty horrible. The sentences are still grammatically correct. But that > doesn't mean it's a good idea to talk like that. LOL!!
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-11-22 08:41 -0500 |
| Message-ID | <roy-36FCE4.08412822112013@news.panix.com> |
| In reply to | #60203 |
In article <528eec7a$0$29992$c3e8da3$5496439d@news.astraweb.com>, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > There are all sorts of things that you can do that don't make your code > "wrong" but do make it difficult to deal with. Why stop with semi-colons? > > import socket; pass; pass; pass; pass; pass; > serverReady = ((True is True) is True) is True) is True); > serverSock = socket . \ > socket( > socket . \ > AF_INET \ > , \ > socket . \ > SOCK_STREAM \ > ) \ > ; Steve, you're just worried about how readable some Python code is. All I can say to that is #firstworldproblem. There's bigger issues at stake here. One thing to be aware of is that semicolons are valuable on the world punctuation spot market. Somewhere, right now, in Greenwich or Stamford, or maybe Tribeca, in some hedge-fund sweat shop, there's a C++ programmer who can't afford to write a for(;;) loop because he doesn't have enough semicolons. Why? Because the world punctuation markets can't handle the added buy-side pressure from new Python programmers using the wrong punctuation. Also, every semicolon we save can be broken down and res-used as TWO decimal points! The Americans use the top part, most other places use the bottom part. It's like a punctuation breeder reactor. One piece goes in, and two come out. So, really. Cut it out with the semicolons. If you won't do it for us, think of the hedge-fund coders.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-11-23 01:16 +1100 |
| Message-ID | <mailman.3039.1385130037.18130.python-list@python.org> |
| In reply to | #60220 |
On Sat, Nov 23, 2013 at 12:41 AM, Roy Smith <roy@panix.com> wrote: > Also, every semicolon we save can be broken down and res-used as TWO > decimal points! The Americans use the top part, most other places use > the bottom part. It's like a punctuation breeder reactor. One piece > goes in, and two come out. That's only the glyph, though. You can't do that with the codepoints - all you end up with is a U+0003 'END OF TEXT' and a U+000B 'LINE TABULATION', useful occasionally but hardly in great demand. No, once you've broken the glyph apart, there's not a lot you can do with the codepoint, and they end up filling the nuclear waste disposal caverns. People keep coming up with schemes for utilizing waste U+0003s, but there's a fundamental problem that text can only end once [1], and all attempts to use U+0003 in commercial use resulted in the premature termination of the text concerned. Of course, the FDA put an immediate stop to that - so expensive to compensate the families of the terminated text - so we're back where we started. ChrisA [1] As is stated in the Holy Writ, Hebrews 9:27: "Just as text is destined to end once, and after that to face judgment, so C strings receive but a single NUL to terminate the strings of many."
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web