1

I am trying to write a function in python 3.x that returns a single byte of data to send over a TCP connection.

The data I am trying to send is an unsigned integer from 0-100.

I am checking the bytes used by the data with sys.getsizeof(data), but I have not come across an instance where sys.getsizeof(data) == 1 .

lets say data = 10

I have tried several different ways to store a byte:

 1) struct.pack('>b', 10)

 2) bytes(10), bytes([10])

 3) 0x10

 4) bytearray(10)

 5) b'10'

all of these have returned over double digits in byte size.

I am looking for a way in python to send a single byte over a TCP socket.

7
  • If you only have a single byte, it doesn't matter if you have dozens of bytes of overhead, because you're only paying that overhead once. You have billions of bytes of memory. Commented Jan 3, 2018 at 17:43
  • Can you explain a little more? Sure its not using those bytes, but I don't want the overhead because of space constraints. Commented Jan 3, 2018 at 17:43
  • Now, if you're calling this function more than once, so you have a bunch of bytes to store, then store them together in the same bytearray. Commented Jan 3, 2018 at 17:44
  • I only want to return a single byte Commented Jan 3, 2018 at 17:45
  • 2
    Python objects don't work like that. You can't just have a sigle byte, or a single machine integer, you always get a fully-fledged object, with the overhead that comes with that. Perhaps you should explain what you're really trying to do. For example, if you need to pass a byte from Python to a library written in C, then there's a way to do that. Commented Jan 3, 2018 at 17:48

2 Answers 2

5

Answering OP's underlying question:

I am trying to send that single byte over a TCP socket.

The Python representation that you use to hold your byte is irrelevant. You don't send a copy of Python object, you send the value contained in the Python object. Since socket.send() requires a buffer or string, I'd have your function return a bytestring: b'\x0a' should work.

Here is a simple client program that sends exactly one byte to a server:

import socket

def return_one_byte():
    return b'\x10'

s = socket.socket()
s.connect(('localhost', 1234))
s.send(return_one_byte())
s.close()
Sign up to request clarification or add additional context in comments.

Comments

3

Python is not like C or C++. Python inherently has high overhead. You can not store data in memory, but instead, get a fully-fledged object that is stored in memory.

When you try things like

 1) struct.pack('>b', 10)
 2) bytes(10), bytes([10])
 3) 0x10
 4) bytearray(10)
 5) b'10'

These are not storing bytes directly to memory, they are creating python objects to store byte data in. Thats why sys.getsizeof(data) != 1.

That being said there is a way around this to answer the underlying question. socket.send() requires a buffer or string and python has byte-strings. so socket.send('\x0a') would send exactly one byte. You could also return a byte string from a function.

def return_byte_string():
    return b'\x0a'

Then this could be passed into socket.send(return_byte_string()) to send a single byte over a TCP connection.

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.