reading from sockets

Discussion in 'Python' started by AndrewTK, Aug 10, 2006.

  1. AndrewTK

    AndrewTK Guest

    Hello,

    I'm trying to read data from a socket and I'm not seeing what I'm
    expecting.... it seems to skip the first line of data. I am new to
    Python and just trying to test what I can do with it... and it's not
    looking pretty.


    I have some Python code:
    [------------------------------
    #! /usr/bin/python

    import socket

    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect( ("localhost",54321) );
    s.send("hello")
    data = s.recv(1024)
    while len(data) > 0:
    print repr(data) # the first print of data always seems to "ignore"
    the first line of data...
    data = s.recv(1024)
    ------------------------------]

    On localhost 54321 I have a server (in Java) writing out

    [------------------------------
    first
    second
    third
    <EOF>
    ------------------------------]
    (on EOF, the stream is simply closed.)

    The result of the script is to print

    [------------------------------
    second
    third
    ------------------------------]

    I have tried already
    [++++++++++++++
    data = " "
    while len(data) > 0:
    data = s.recv(1024)
    print repr(data)
    ++++++++++++++]

    but that has not changed anything. The first line to come through is
    always skipped it would seem.

    Any ideas as to why that would be....? I was thinking scope when I made
    the last example, but apparently that is not the case.

    Andrew
     
    AndrewTK, Aug 10, 2006
    #1
    1. Advertisements

  2. AndrewTK

    Simon Forman Guest

    Hmm.. that's very very strange.

    Try putting "print repr(data)" immediately before your while statement
    and after the first recv() call to see... well, actually, nevermind
    that.

    Double check that the first line really is being sent (with netcat or
    telnet or something.) That's the only thing I can think of, your code
    should be working.. If the first line is really being sent, but not
    arriving in your code, you've got something strange going on.

    (BTW, "while len(data) > 0:" can just be "while data:")

    (Heaven help me, but maybe you could post your java server code here...
    If anyone screams too loudly blame me. Hahaha..)

    Peace,
    ~Simon
     
    Simon Forman, Aug 10, 2006
    #2
    1. Advertisements

  3. AndrewTK

    AlbaClause Guest

    I'm new to Python too, but I'd have to agree with Simon on this: There's
    probably something screwy in your java code.

    I'm assuming that your server waits to receive the word 'hello' before
    replying with the three strings (first, second, and third)? So once your
    script sends the word 'hello', there's nothing for it to do but wait for a
    response. And that's exactly what your script does.
     
    AlbaClause, Aug 11, 2006
    #3
  4. AndrewTK

    AndrewTK Guest

    I'm assuming that your server waits to receive the word 'hello' before
    Nope - actually it's a threaded "server", with the main thread simply
    dumping network input to the console and command line input being
    directly dumped to the network output stream.

    I confess to having typed the three lines manually...!

    It's at:
    http://www.dcs.st-and.ac.uk/~atk1/perso/java/servers/RespondingServer.java

    It launches on the command line with
    java RespondingServer _port_

    _port_ being the port number it should listen for data on.
     
    AndrewTK, Aug 12, 2006
    #4
  5. AndrewTK

    AndrewTK Guest

    AndrewTK, Aug 12, 2006
    #5
  6. AndrewTK

    Simon Forman Guest

    Hooooy. I tried reading your Java code.. What can I say? There's a
    reason why I like Python... :)

    I couldn't see anything obviously broken in your server, but there's
    definitely nothing wrong with your python code, except that you call
    flush() on the socket (fixed in your posted code but not in the linked
    code.)

    Here are the results of testing, with netcat substituted for your java
    server:

    # on the server side:
    [email protected]:~ $ netcat -p 54321 -l
    hellohi there
    how are you
    I'm fine thanks

    [email protected]:~ $



    # on the client side:
    [email protected]:~ $ python delme.py
    'hi there\n'
    'how are you\n'
    "I'm fine thanks\n"
    [email protected]:~ $


    So I'm guessing it's something wrong in your java server.

    HTH,
    ~Simon
     
    Simon Forman, Aug 12, 2006
    #6
  7. AndrewTK

    AndrewTK Guest

    Thanks then. I'll keep testing then...
     
    AndrewTK, Aug 13, 2006
    #7
  8. AndrewTK

    AndrewTK Guest

    Thanks then. I'll keep testing then... Although I don't seem to have
    netcat on my unit...

    I'm using a uni computer so I can't install stuff... but I'm guessing
    what I wrote is something like a basic-basic thingy that does what
    netcat is designed to do.....?
     
    AndrewTK, Aug 13, 2006
    #8
  9. AndrewTK

    Simon Forman Guest

    First link from googling netcat:
    http://netcat.sourceforge.net/

    Online man page for netcat (a.k.a. nc)
    http://www.openbsd.org/cgi-bin/man.cgi?query=nc
    (Note, it's the openbsd man page. YMMV)

    If you can compile C on your university computer then you should be
    able to download, compile, and "install" it to a dir in your homedir.
    I've done it, so I know it's possible. :) (I was working on a remote
    machine that I didn't have root on, and I needed to test a tcp server I
    was writing.)

    Netcat's a truly awesome and surprisingly useful little tool. Any
    effort you spend to learn it will be well repaid.

    Peace,
    ~Simon
     
    Simon Forman, Aug 13, 2006
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.