stuck at this from so much time,need help....please ..

B

Bhanu Karthik

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if command == "/quit":
print data
sock.send("bye")
sock.close()
CONNECTION_LIST.remove(sock)

even if the received data is '/quit' the if condition not excuting...please help.
 
C

Chris Angelico

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if command == "/quit":
print data
sock.send("bye")
sock.close()
CONNECTION_LIST.remove(sock)

even if the received data is '/quit' the if condition not excuting...please help.

At what point is command set? You're setting data here; is command
supposed to be derived from data?

This looks like a MUD or IRC style of server, which would suggest that
commands are terminated by end-of-line. You may need to take content
from the socket (currently in data) and split it off on either "\r\n"
or "\n". But it's hard to tell from this small snippet.

ChrisA
 
B

Bhanu Karthik

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if command == "/quit":
print data




even if the received data is '/quit' the if condition not excuting...please help.



At what point is command set? You're setting data here; is command

supposed to be derived from data?



This looks like a MUD or IRC style of server, which would suggest that

commands are terminated by end-of-line. You may need to take content

from the socket (currently in data) and split it off on either "\r\n"

or "\n". But it's hard to tell from this small snippet.



ChrisA

sorry its not command its data....

I miss wrote it here...
 
C

Chris Angelico

sorry its not command its data....

I miss wrote it here...

Okay. Start by copying and pasting your actual code, and saying what
you're doing to trigger it. If this is a stream socket (eg TCP), you
have no way of knowing where one read will end and the next start, so
you'll need to do some kind of buffering and splitting.

Also, please use something better than Google Groups; your posts are
looking rather ugly, and it's the fault of your client rather than
yourself.

ChrisA
 
B

Bhanu Karthik

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if command == "/quit":
print data




even if the received data is '/quit' the if condition not excuting...please help.



At what point is command set? You're setting data here; is command

supposed to be derived from data?



This looks like a MUD or IRC style of server, which would suggest that

commands are terminated by end-of-line. You may need to take content

from the socket (currently in data) and split it off on either "\r\n"

or "\n". But it's hard to tell from this small snippet.



ChrisA

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if data == "/quit":
print data
sock.send("bye")
sock.close()
CONNECTION_LIST.remove(sock)


this is exact code..
it is not even entering the if ...
I tried ( c= (data is '/quit')if c)

when i print c ,its printing false....I dont understand what is happening...please help..
 
R

Roy Smith

Bhanu Karthik said:
data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if data == "/quit":
print data
sock.send("bye")
sock.close()
CONNECTION_LIST.remove(sock)


this is exact code..
it is not even entering the if ...
I tried ( c= (data is '/quit')if c)

That can't be the exact code. What you posted is a syntax error because
the line after the "if" statement isn't indented properly.
 
B

Bhanu Karthik

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if data == "/quit":
print data


CONNECTION_LIST.remove(sock)


this is exact code..
it is not even entering the if ...
I tried ( c= (data is '/quit')if c)



That can't be the exact code. What you posted is a syntax error because

the line after the "if" statement isn't indented properly.

indentation is correct when I trying to paste it here,it is showing like it is unindented.
 
C

Chris Angelico

this is exact code..
it is not even entering the if ...
I tried ( c= (data is '/quit')if c)

when i print c ,its printing false....I dont understand what is happening...please help..

Again, please get off Google Groups. Have a look at how your posts come out:

https://mail.python.org/pipermail/python-list/2013-November/661005.html

Note how your quoted text is double-spaced. This is one of the most
obvious and obnoxious problems, though not the only one.

You have two problems here. Firstly, comparing strings with 'is' is
wrong in most cases[1], so stick with ==. And secondly, my crystal
ball says that you're typing "/quit" and pressing enter, so your
socket read will be "/quit\r\n" or "/quit\n". To properly handle this,
you'll need to buffer and split as I was describing.

ChrisA
 
C

Chris Angelico

indentation is correct when I trying to paste it here,it is showing like it is unindented.

That's because Google Groups mucks things up. Get a better client.

ChrisA
 
M

MRAB

data = sock.recv(RECV_BUFFER)
username = str(sock.getpeername())
username = usernames[username]
if command == "/quit":
print data
sock.send("bye")
sock.close()
CONNECTION_LIST.remove(sock)

even if the received data is '/quit' the if condition not
excuting...please help.

At what point is command set? You're setting data here; is command
supposed to be derived from data?

This looks like a MUD or IRC style of server, which would suggest that
commands are terminated by end-of-line. You may need to take content
from the socket (currently in data) and split it off on either "\r\n"
or "\n". But it's hard to tell from this small snippet.

ChrisA

sorry its not command its data....

I miss wrote it here...
If the 'if' condition not executing then it must be because the
received data isn't "/quit", so what is it? Print it out, or, even
better, print out repr(data), which will show you any spaces or "\n" in
it.

sock.recv(RECV_BUFFER) will receive up to RECV_BUFFER bytes, so if the
sender is sending other stuff after the "/quit" and RECV_BUFFER > 5,
then you might be receiving some of that later stuff too.
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top