newbie question

Discussion in 'Python' started by jeff, Apr 15, 2004.

  1. jeff

    jeff Guest

    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?
    jeff, Apr 15, 2004
    #1
    1. Advertising

  2. jeff wrote:

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

    --
    Stach Jabber: stach at jabber atman pl
    Krzysztof Stachlewski, Apr 15, 2004
    #2
    1. Advertising

  3. jeff

    mr_organic Guest

    jeff <plastic"no-spam-pls"@xsintrk.net> wrote in
    news:Xns94CCA389D3DA0plasticnospamplsxsin@216.77.188.18:

    > 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
    mr_organic, Apr 15, 2004
    #3
  4. jeff

    jeff Guest

    Krzysztof Stachlewski <> wrote in
    news:c5mtsl$dns$:

    > jeff wrote:
    >
    >> 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?
    >

    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
    jeff, Apr 15, 2004
    #4
  5. jeff

    Adam Guest

    On Thu, 15 Apr 2004 20:55:11 GMT
    jeff <plastic"no-spam-pls"@xsintrk.net> wrote:

    > 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
    Adam, Apr 15, 2004
    #5
  6. jeff

    Larry Bates Guest

    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.

    "jeff" <plastic"no-spam-pls"@xsintrk.net> wrote in message
    news:Xns94CCA82D89C84plasticnospamplsxsin@216.77.188.18...
    > Krzysztof Stachlewski <> wrote in
    > news:c5mtsl$dns$:
    >
    > > jeff wrote:
    > >
    > >> 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?
    > >

    > 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
    Larry Bates, Apr 15, 2004
    #6
  7. jeff wrote:

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

    --
    Stach Tlen: stachobywatelpl, GG: 1811474
    Jabber: stach at jabber atman pl
    Krzysztof Stachlewski, Apr 15, 2004
    #7
  8. jeff

    jeff Guest

    Krzysztof Stachlewski <> wrote in
    news:c5mvrg$fda$:

    > 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().
    jeff, Apr 15, 2004
    #8
  9. "jeff" <plastic"no-spam-pls"@xsintrk.net> wrote in message
    news:Xns94CCAEBD1A18Eplasticnospamplsxsin@216.77.188.18...

    > 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
    Krzysztof Stachlewski, Apr 16, 2004
    #9
  10. jeff

    jeff Guest

    "Krzysztof Stachlewski" <> wrote in
    news:c5nuno$1mj$:

    > 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


    *smacks forehead*
    *hard*
    jeff, Apr 16, 2004
    #10
  11. "jeff" <plastic"no-spam-pls"@xsintrk.net> wrote in message
    news:Xns94CCAEBD1A18Eplasticnospamplsxsin@216.77.188.18...

    > 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.
    Richard Brodie, Apr 16, 2004
    #11
  12. jeff

    jeff Guest

    "Richard Brodie" <> wrote in
    news:c5occn$:

    > 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?
    jeff, Apr 16, 2004
    #12
  13. jeff

    Chris Green Guest

    jeff <plastic"no-spam-pls"@xsintrk.net> writes:

    > 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

    --
    Chris Green <>
    TCP: Treason uncloaked!
    Chris Green, Apr 16, 2004
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry C.
    Replies:
    8
    Views:
    214
    Uri Guttman
    Nov 23, 2003
  2. Kruno Saho
    Replies:
    0
    Views:
    131
    Kruno Saho
    Apr 7, 2013
  3. Dave Angel
    Replies:
    0
    Views:
    113
    Dave Angel
    Apr 7, 2013
  4. rusi
    Replies:
    0
    Views:
    102
  5. Miki Tebeka
    Replies:
    0
    Views:
    76
    Miki Tebeka
    Apr 7, 2013
Loading...

Share This Page