0

I'm new to python, and I'm trying to use a 3rd party library/module. What I'm doing now is:

s.send(rtp.header_bytes + rtp.payload) 
# -> \x80!\x00\x01\x00\x00\x00d\x00\x00\x00\x00Testy

to send the header and the payload of a packet via tcp socket. On the receiving side:

conn, addr = socket_rtp.accept()
data = conn.recv(1024)

the data is passed to a constructor __init__(self, bytes, length) and then used like this

self.version = (bytes[0] & Packet.V_MASK) >> 6

but it seems that it's interpreting the bytes array as string? I'm getting the following exception:

.... line 322, in __init__
    self.version = (bytes[0] & Packet.V_MASK) >> 6
TypeError: unsupported operand type(s) for &: 'str' and 'int'

Thank you for your help.

1 Answer 1

1

To convert the string (bytes) to int, you can use ord (assuming your're using Python 2.x):

self.version = (bytes[0] & Packet.V_MASK) >> 6

or struct.unpack:

self.version = (struct.unpack('B', bytes[0])[0] & Packet.V_MASK) >> 6

>>> ord('\x80')
128
>>> import struct
>>> struct.unpack('B', '\x80')[0]
128
Sign up to request clarification or add additional context in comments.

2 Comments

This gives me: Struct() argument 1 must be string, not unicode. Also, I'm now confused why there is a '!' in the string representation...
@damian, Did you import __future__ (from __future__ import unicode_literal)? Then, use b'B' instead of 'B'.

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.