Having trouble setting up an extremely simple server...

C

cilantromc

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 sendanother 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 thingsdown.

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;
 
R

Roy Smith

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.
 
C

Cilantro MC

(e-mail address removed) 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




import socket;
serverReady = True;

serverSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
serverSock.bind(('localhost', 8081));


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;

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?
 
R

Roy Smith

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.
[/QUOTE]

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().
 
N

Ned Batchelder

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.
 
G

Gregory Ewing

Cilantro said:
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. :)
 
C

Chris Angelico

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/
 
S

Steven D'Aprano

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.
 
R

rusi

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!!
 
R

Roy Smith

Steven D'Aprano said:
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.
 
C

Chris Angelico

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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top