newbie question

J

jeff

ex:

a = "text str"
if a == "text str": print a

why doesn't that work? i couldn't find much help about variable types in
the manual, does it need a property like a.text?
 
K

Krzysztof Stachlewski

jeff said:
ex:

a = "text str"
if a == "text str": print a

why doesn't that work? i couldn't find much help about variable types in
the manual, does it need a property like a.text?

It works - at least on my computer.
That is: it prints "text str" to the console.
What did you expect it to do?
 
M

mr_organic

ex:

a = "text str"
if a == "text str": print a

why doesn't that work? i couldn't find much help about variable types
in the manual, does it need a property like a.text?

If you're at the commandline, remember to push ENTER *twice* at the end of
your last statement; a blank line causes the interpreter to execute your
code. I just used your code and it worked fine.

mr_organic
 
J

jeff

It works - at least on my computer.
That is: it prints "text str" to the console.
What did you expect it to do?
ok this is the bit i'm working with, i'm tinkering with sockets.
when the recieved text == kill, i want the socket to close. actually, the
example i posted worked for me too, but i don't understand why this
doesn't.

while 1:
sdata = s.recv(1024)
if not sdata: break
if sdata == "kill": s.close()
print "Recieved:", sdata
 
A

Adam

ex:

a = "text str"
if a == "text str": print a

why doesn't that work? i couldn't find much help about
variable types in the manual, does it need a property like
a.text?

What error are you getting?
adam
 
L

Larry Bates

sdata may have additional characters in it. If so,
there is no way it will ever equal "kill". You
might want something like:

if sdata.startswith("kill"): s.close()

assuming that kill will be at the front of the data
stream of 1024 characters.

Larry Bates
Syscon, Inc.
 
K

Krzysztof Stachlewski

jeff said:
ok this is the bit i'm working with, i'm tinkering with sockets.
when the recieved text == kill, i want the socket to close. actually, the
example i posted worked for me too, but i don't understand why this
doesn't.

while 1:
sdata = s.recv(1024)
if not sdata: break
if sdata == "kill": s.close()
print "Recieved:", sdata

First of all, you can't be sure that you will receive the text "kill"
in one piece. It could happen that one recv() call will give only
"ki" and the following recv() will return the rest of it.
Moreover - try to print repr(sdata) and see what *exactly* is received.
 
J

jeff

First of all, you can't be sure that you will receive the text "kill"
in one piece. It could happen that one recv() call will give only
"ki" and the following recv() will return the rest of it.
Moreover - try to print repr(sdata) and see what *exactly* is
received.

i only want the socket to close if the word 'kill' has been sent.
i just tried 'if sdata == "kill\r\n": s.close()', kill\r\n was the string
repr(sdata) returned. the IF statement works, but i'm getting a 'bad file
descriptor' error on the s.close().
 
K

Krzysztof Stachlewski

i only want the socket to close if the word 'kill' has been sent.
i just tried 'if sdata == "kill\r\n": s.close()', kill\r\n was the string
repr(sdata) returned. the IF statement works, but i'm getting a 'bad file
descriptor' error on the s.close().

This is because you are trying to use a socket that is already closed.
You are calling s.close() but you are not break-ing out of the loop.
In the next iteration s.recv() is called, but s is closed
at that moment. Add a break after the call to s.close().

Stach
 
J

jeff

This is because you are trying to use a socket that is already closed.
You are calling s.close() but you are not break-ing out of the loop.
In the next iteration s.recv() is called, but s is closed
at that moment. Add a break after the call to s.close().

*smacks forehead*
*hard*
 
R

Richard Brodie

i only want the socket to close if the word 'kill' has been sent.

You are missing Krzysztof's point. Even if the string 'kill' is sent,
TCP does not guarantee that is what you will receive. It is a pure
stream system and has no concept of records or lines; that isn't likely
the problem here but sooner or later, you *will* get burnt if you start
thinking that if 4 characters are sent, then 4 will be received.
 
J

jeff

You are missing Krzysztof's point. Even if the string 'kill' is sent,
TCP does not guarantee that is what you will receive. It is a pure
stream system and has no concept of records or lines; that isn't likely
the problem here but sooner or later, you *will* get burnt if you start
thinking that if 4 characters are sent, then 4 will be received.

how can i be sure i have received the entire string?
 
C

Chris Green

jeff said:
how can i be sure i have received the entire string?

You need to queue up the data returned and have a protocol defined for
when a command terminates such as '\r\n'. Ugly Untested code below.

buf = ""
delim = "\r\n"

while 1:
sdata = s.recv(1024)

if not sdata:
break

# append sdata to the buf
buf += sdata

while 1:
idx = buf.find(delim)

if idx > -1:
cmd = buf[:idx] # grab cmd
buf = buf[:idx + len(delim)] # save off the rest of the command

if cmd == "kill":
s.close()
print "Received:", sdata
else:
break # breaks inner loop
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top