I am setting up a socket with TCP/IP-protocol, and since my receiver is handling int8u_t
i would like to know if this approach is correct.
At connection the server has to send a value mode=int(42)
to the receiver which is done in def connectionMade(self)
. But i understand there will be some conflicts since the normal int in python is 32-bit and my receiver is only 8-bit, can i somehow cast it or create it in int8u?
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
class TestSocket(Protocol):
def connectionMade(self):
mode=int(42)
self.factory.clients.append(self)
self.transport.write(mode)
print "clients are ", self.factory.clients
def connectionLost(self, reason):
self.factory.clients.remove(self)
def dataReceived(self, data):
#print "data is ", data
#a = data.split(':')
print data
print "-------------------"
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = TestSocket()
factory.clients = []
reactor.listenTCP(30002, factory)
print "TestSocket server started"
reactor.run()
Use struct
from struct import *
mode = pack("h", 42) # 'h' == short
edit: Apparently you wanted pack("I", 42)