1

I found this example in stackoverflow (link) from user3132092 for a TCP Client sending a "Hello world" string via TCP:

Sending string

import socket

host = socket.gethostname()
port = 12345                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(b'Hello, world')

The question is how does s.sendall() accepts a string as parameter the same way it accepts a binary? Is the "Hello world" converted to binary?

In this example (link) s.sendall(link) is sending a binary that was converted previously to binary using python struct. Why is the struct not necessary for the "Hello World"?

Sending binary

import struct

values = (1, 'ab', 2.7)
packer = struct.Struct('I 2s f')
packed_data = packer.pack(*values)

print >>sys.stderr, 'sending "%s"' % binascii.hexlify(packed_data), values
sock.sendall(packed_data)
0

3 Answers 3

1

You are using Python 2, and the b'...' syntax does not actually produce a separate object type:

>>> import sys
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=13, releaselevel='final', serial=0)
>>> b'This is just a string'
'This is just a string'
>>> 'This is also just a string'
'This is also just a string'
>>> type(b'Just a string')
<type 'str'>
>>> type('Just a string')
<type 'str'>

The b'...' syntax only exists in Python 2 to make it easier to build code that works on Python 2 and 3 without changes.

The struct module also produces str string objects:

>>> import struct
>>> values = (1, 'ab', 2.7)
>>> packer = struct.Struct('I 2s f')
>>> packed_data = packer.pack(*values)
>>> packed_data
'\x01\x00\x00\x00ab\x00\x00\xcd\xcc,@'
>>> type(packed_data)
<type 'str'>

As such, you are always sending a str string object, there is no magic involved here. In Python 2, the str object is just a series of bytes, really.

Sign up to request clarification or add additional context in comments.

Comments

0

You have to specify which python you are using, sendall() in Python 2 and 3 are different, python2 is expecting string but python3 is expecting bytes instead.

Be aware that in python2:

socket.sendall(string[, flags]) Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Unlike send(), this method continues to send data from string until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

Python3:

socket.sendall(bytes[, flags]) Send data to the socket. The socket must be connected to a remote socket. The optional flags argument has the same meaning as for recv() above. Unlike send(), this method continues to send data from bytes until either all data has been sent or an error occurs. None is returned on success. On error, an exception is raised, and there is no way to determine how much data, if any, was successfully sent.

2 Comments

They are using Python 2, so b'...' is not a bytestring but a regular str object.
@MartijnPieters your're right, thanks. I will remove my answer!
0

If you read over the page you've linked with the struct sendall example, you'll see...

values = (1, 'ab', 2.7)
packer = struct.Struct('I 2s f')

...results in...

sending "0100000061620000cdcc2c40" (1, 'ab', 2.7)

So it's sending an integer (1 / I), followed by a two-character string ('ab' / 2s), and finally a floating point value (2.7 / f). Those are packed contiguously in binary form, with the integer and floating point values sent using the same bits used to store them in memory - rearranged if necessary to a portable machine-independent "network" endian ordering so the other side will be able to unpack them - rather than converting them to human-readable text. The 6162 in the middle of the sent data is the ASCII encoding of 'ab'. 61 hex is 'a', and 62 'b'. The struct serves to assemble all these three parts together, but if you're just sending a string of textual data you don't need it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.