Python Forum
Send raw string via socket with out any changes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Send raw string via socket with out any changes
#11
(Jul-24-2019, 11:05 AM)Gribouillis Wrote: What I see in the code is that you're sending the output of con_dat() through the socket
labeler_datasend = con_dat(aa_labeler_frame)
...
labeler_socket.send(labeler_datasend)
I don't understand what con_dat() does but I see that it returns the python type str and not a type bytes because there are statements such as rawconvertdata = '' (instead of = b'') or like rawhexstring = str(rawdata), etc. So the first thing to do seems to clean up this function so that it returns the binary string that the labeller is expecting.

con_dat - it will be another def, i don't have it yet


For example: its my string:
data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\x4f\x52\x44\x45\x52\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x09\x31\x38\x39\x00\x64\x75\x70\x6b\x61\x5f\x69\x77\x6f\x6e\x6b\x69\x00\x31\x35\x39\x30\x30\x34\x37\x37\x30\x30\x32\x32\x38\x32\x38\x35\x38\x36\x00\x30\x35\x39\x30\x30\x34\x37\x37\x30\x30\x32\x36\x32\x38\x00\x38\x31\x00\x32\x30\x31\x32\x30\x30\x00\x34\x31\x30\x31\x38\x39\x00\x73\x6f\x63\x31\x31\x32\x30\x36\x31\x39\x00\x31\x32\x33\x34\x35\x36\x37\x38\x39\x30\x00\xe7\xdc'

and my string in TCP frame must look like below:
[Image: dMh0d4x]
Reply
#12
Again, you need a bytes type, so if you hard-code the string, dont use
data = '\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
but use
data = b'\x01\x81\xaa\x76\x00\x5d\x44\x00\x00\x00\x00\x57\x47\x5f\...'
Now I don't know exactly what the question is. If the labeler doesn't work with your data, you could prehaps print the data before you send it to the labeler and see if it's the correct data. For example use
print('SENDING:', repr(labeler_datasend))
labeler_socket.send(labeler_datasend)
Reply
#13
I solved my problem:
def rawbytes(s):
    """Convert a string to raw bytes without encoding"""
    outlist = []
    for cp in s:
        num = ord(cp)
        if num < 255:
            outlist.append(struct.pack('B', num))
        elif num < 65535:
            outlist.append(struct.pack('>H', num))
        else:
            b = (num & 0xFF0000) >> 16
            H = num & 0xFFFF
            outlist.append(struct.pack('>bH', b, H))
    return b''.join(outlist)
Reply
#14
I'm glad it works, but I still believe you're doing something that's not needed. First I don't see why you need to convert a string to bytes in the first place. If the labeler needs bytes, you should be able to generate this data without intermediary data of type str. Why would one use a string if a bytes is needed?
Reply
#15
(Jul-25-2019, 08:06 AM)Gribouillis Wrote: I'm glad it works, but I still believe you're doing something that's not needed. First I don't see why you need to convert a string to bytes in the first place. If the labeler needs bytes, you should be able to generate this data without intermediary data of type str. Why would one use a string if a bytes is needed?

I know, but i start write program and just later see problem string-->byte and know i haven't any time,

but thank You for Your help.
Reply
#16
data in the source code needs to be put in quotes. use a type of quote different than any quotes that are part of the data. make the string be bytes type by putting the b just before the starting quote. do not put r in front because that kind of raw will make your \x stuff not work.

for what you are doing you need binary. that is what the bytes type is. your use of the term "raw" for this is not the same meaning as python's meaning of "raw". you do not need python's raw. so don't even think of trying to use it. just use bytes. and bytes literal looks like b'this is bytes'.

are you using python version 3? you need to be. bytes are not real bytes in version 2.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
(Jul-27-2019, 12:53 AM)Skaperen Wrote: data in the source code needs to be put in quotes. use a type of quote different than any quotes that are part of the data. make the string be bytes type by putting the b just before the starting quote. do not put r in front because that kind of raw will make your \x stuff not work.

for what you are doing you need binary. that is what the bytes type is. your use of the term "raw" for this is not the same meaning as python's meaning of "raw". you do not need python's raw. so don't even think of trying to use it. just use bytes. and bytes literal looks like b'this is bytes'.

are you using python version 3? you need to be. bytes are not real bytes in version 2.
yes, python3.7.2
Reply
#18
good, version 3. so you started writing some parts before you realized the need to use bytes and the quick workaround is to convert strings to bytes. i've had a similar issue before and did the conversion like this:
  my_bytes = bytes([ord(x) for x in my_string])
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#19
(Jul-30-2019, 08:04 PM)Skaperen Wrote: good, version 3. so you started writing some parts before you realized the need to use bytes and the quick workaround is to convert strings to bytes. i've had a similar issue before and did the conversion like this:
  my_bytes = bytes([ord(x) for x in my_string])

It's working, Thank You!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send data BMP180 between client and server trought module socket smalhao 0 2,807 Jul-30-2018, 12:56 PM
Last Post: smalhao
  Simple send and recive string Server/Client Epilepsy 1 2,716 May-01-2018, 08:17 PM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020