sending binary data over sockets

Discussion in 'Python' started by thorley@gmail.com, Jul 3, 2006.

  1. Guest

    Greetings, since there was no reponse to my previous post about an
    existing FastCGI server in python, I've taken to writing my own. (which
    of course I'll share--*if* there's something to share ;)

    My problem now, is that I need to send certain binary data over a
    socket. That is, I want to make some bytes, and stuff them in a TCP
    packet, send them down the pipe, and then listen for a response.

    socket.send, as best I can tell, will only send strings. I've read on
    the list other conversations where the recommendation was to use xdrlib
    or struct. But it appears that is only useful when you control the
    client and the server. PLEASE correct me if I'm wrong, but using struct
    just encodes the binary data as a string, right?

    # Example sending binary data as a string
    s = socket.socket()
    s.connect(("127.0.0.1", 8000))
    packet = struct.pack('4B', 1, 2, 3, 4)
    s.send(packet)

    In my understaing the above just sends the string '\x01\x02\x03\x04',
    not raw binary data. I've looked at Billy the Kid and pcap, which are
    cool but not what I need.

    Do I have to build my own packets from scratch and encode all the
    TCP/IP headers myself to get this to work?

    Solutions and Corrections welcome and appreciated.
    Thanks very much

    --
    matthew
     
    , Jul 3, 2006
    #1
    1. Advertising

  2. schrieb:
    > Greetings, since there was no reponse to my previous post about an
    > existing FastCGI server in python, I've taken to writing my own. (which
    > of course I'll share--*if* there's something to share ;)
    >
    > My problem now, is that I need to send certain binary data over a
    > socket. That is, I want to make some bytes, and stuff them in a TCP
    > packet, send them down the pipe, and then listen for a response.
    >
    > socket.send, as best I can tell, will only send strings. I've read on
    > the list other conversations where the recommendation was to use xdrlib
    > or struct. But it appears that is only useful when you control the
    > client and the server. PLEASE correct me if I'm wrong, but using struct
    > just encodes the binary data as a string, right?


    WHich is perfectly alright, as
     
    Diez B. Roggisch, Jul 3, 2006
    #2
    1. Advertising

  3. Premature sending syndrome...

    Diez B. Roggisch schrieb:
    > schrieb:
    >> Greetings, since there was no reponse to my previous post about an
    >> existing FastCGI server in python, I've taken to writing my own. (which
    >> of course I'll share--*if* there's something to share ;)
    >>
    >> My problem now, is that I need to send certain binary data over a
    >> socket. That is, I want to make some bytes, and stuff them in a TCP
    >> packet, send them down the pipe, and then listen for a response.
    >>
    >> socket.send, as best I can tell, will only send strings. I've read on
    >> the list other conversations where the recommendation was to use xdrlib
    >> or struct. But it appears that is only useful when you control the
    >> client and the server. PLEASE correct me if I'm wrong, but using struct
    >> just encodes the binary data as a string, right?


    Python strings are binary data and can contain
    - in oppostion to e.g. C-strings - null-bytes.

    So it is perfectly alright to send "only" strings over a socket.

    Diez
     
    Diez B. Roggisch, Jul 3, 2006
    #3
  4. Sukanta Guest

    wrote:
    > Greetings, since there was no reponse to my previous post about an
    > existing FastCGI server in python, I've taken to writing my own. (which
    > of course I'll share--*if* there's something to share ;)
    >
    > My problem now, is that I need to send certain binary data over a
    > socket. That is, I want to make some bytes, and stuff them in a TCP
    > packet, send them down the pipe, and then listen for a response.
    >
    > socket.send, as best I can tell, will only send strings. I've read on
    > the list other conversations where the recommendation was to use xdrlib
    > or struct. But it appears that is only useful when you control the
    > client and the server. PLEASE correct me if I'm wrong, but using struct
    > just encodes the binary data as a string, right?
    >
    > # Example sending binary data as a string
    > s = socket.socket()
    > s.connect(("127.0.0.1", 8000))
    > packet = struct.pack('4B', 1, 2, 3, 4)
    > s.send(packet)
    >
    > In my understaing the above just sends the string '\x01\x02\x03\x04',
    > not raw binary data. I've looked at Billy the Kid and pcap, which are
    > cool but not what I need.
    >


    It will send the 4 bytes, binary, and not the string as you assumed. If
    you want to satisfy yourself, run tcpdump (or ethereal) to observe what
    is being sent.


    > Do I have to build my own packets from scratch and encode all the
    > TCP/IP headers myself to get this to work?
    >
    > Solutions and Corrections welcome and appreciated.
    > Thanks very much
    >
    > --
    > matthew
     
    Sukanta, Jul 3, 2006
    #4
  5. On 2006-07-03, <> wrote:

    > My problem now, is that I need to send certain binary data over a
    > socket. That is, I want to make some bytes, and stuff them in a TCP
    > packet, send them down the pipe, and then listen for a response.
    >
    > socket.send, as best I can tell, will only send strings. I've read on
    > the list other conversations where the recommendation was to use xdrlib
    > or struct. But it appears that is only useful when you control the
    > client and the server. PLEASE correct me if I'm wrong, but using struct
    > just encodes the binary data as a string, right?


    Right. Strings are binary data.

    > # Example sending binary data as a string
    > s = socket.socket()
    > s.connect(("127.0.0.1", 8000))
    > packet = struct.pack('4B', 1, 2, 3, 4)
    > s.send(packet)
    >
    > In my understaing the above just sends the string '\x01\x02\x03\x04',
    > not raw binary data.


    The string '\x01\x02\x03\x04' consists of four octets having
    the values 0x01, 0x02, 0x03, 0x04.

    Are those not the four octets you wanted to send?

    --
    Grant Edwards grante Yow! I want EARS! I
    at want two ROUND BLACK
    visi.com EARS to make me feel warm
    'n secure!!
     
    Grant Edwards, Jul 3, 2006
    #5
  6. Guest

    > Python strings are binary data and can contain
    > - in oppostion to e.g. C-strings - null-bytes.
    >
    > So it is perfectly alright to send "only" strings over a socket.


    Agreed. I wasn't trying to imply it was 'wrong' just that the receiving
    application wouldn't interpret strings correctly.
     
    , Jul 3, 2006
    #6
  7. Guest

    > It will send the 4 bytes, binary, and not the string as you assumed. If
    > you want to satisfy yourself, run tcpdump (or ethereal) to observe what
    > is being sent.


    Thanks very much for the prompt reply. I'll take your word for it. I
    actually tried ethereal to verify my hypothesis before posting, but
    wasn't able to see the raw data bytes. That's probably just a
    deficiancy in my ability to steer ethereal.

    thanks again
    --
    matthew
     
    , Jul 3, 2006
    #7
  8. Guest

    > Are those not the four octets you wanted to send?

    Yes. My understanding of struct was broken. Sukanta corrected it for
    me.

    thank you
    --
    matthew
     
    , Jul 3, 2006
    #8
  9. wrote:
    > Greetings, since there was no reponse to my previous post about an
    > existing FastCGI server in python, I've taken to writing my own. (which
    > of course I'll share--*if* there's something to share ;)
    >
    > My problem now, is that I need to send certain binary data over a
    > socket. That is, I want to make some bytes, and stuff them in a TCP
    > packet, send them down the pipe, and then listen for a response.


    As everyone else has already told you, you have a misconception about
    strings and struct and so on. However, if what you want is mutability
    in a pattern, read the array module documentation as well.

    --Scott David Daniels
     
    Scott David Daniels, Jul 3, 2006
    #9
  10. Simon Forman Guest

    Grant Edwards wrote:
    > On 2006-07-03, <> wrote:
    >
    > > My problem now, is that I need to send certain binary data over a
    > > socket. That is, I want to make some bytes, and stuff them in a TCP
    > > packet, send them down the pipe, and then listen for a response.

    ....
    > > In my understaing the above just sends the string '\x01\x02\x03\x04',
    > > not raw binary data.

    >
    > The string '\x01\x02\x03\x04' consists of four octets having
    > the values 0x01, 0x02, 0x03, 0x04.
    >
    > Are those not the four octets you wanted to send?
    >
    > --
    > Grant Edwards


    i.e.
    >>> len('\x01\x02\x03\x04')

    4
     
    Simon Forman, Jul 4, 2006
    #10
    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. Steve Horsley

    Re: sending objects over sockets

    Steve Horsley, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,163
    Steve Horsley
    Jun 27, 2003
  2. Steve Horsley

    Re: sending objects over sockets

    Steve Horsley, Jun 27, 2003, in forum: Java
    Replies:
    0
    Views:
    1,434
    Steve Horsley
    Jun 27, 2003
  3. Knute Johnson

    Re: sending objects over sockets

    Knute Johnson, Jun 27, 2003, in forum: Java
    Replies:
    5
    Views:
    3,253
    Steve Horsley
    Jun 28, 2003
  4. Gilles Dubochet

    Re: Sending bytes and string over sockets

    Gilles Dubochet, Aug 26, 2003, in forum: Java
    Replies:
    0
    Views:
    478
    Gilles Dubochet
    Aug 26, 2003
  5. Walter Huf

    Sending binary data over CGI

    Walter Huf, Mar 4, 2004, in forum: Python
    Replies:
    3
    Views:
    1,251
    Walter Huf
    Mar 4, 2004
Loading...

Share This Page