i am having trouble and a lot of questions about socket programming attached code below ( all parts have been taken from the and written together) i am trying to send mouse data to the client ,howver getting the error:
Traceback (most recent call last):
File "srvr.py", line 29, in <module>
serv.sendall(status)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not int
Questions: 1.how do we send data over sockets other then strings or specifically without the .send("...") statement .continously changing data?? 2.what things should be kept in mind when sending stream of data?? 3.code written here is a mess, would gladly appreciate help to teach some code sense into me
Thank YOu
Code: server side:
from socket import * #import the socket library
##let's set up some constants
HOST = '' #we are the host
PORT = 29876 #arbitrary port not currently in use
ADDR = (HOST,PORT) #we need a tuple for the address
BUFSIZE = 4096 #reasonably sized buffer for data
## now we create a new socket object (serv)
## see the python docs for more information on the socket types/flags
serv = socket( AF_INET,SOCK_STREAM)
##bind our socket to the address
serv.bind((ADDR)) #the double parens are to create a tuple with one element
serv.listen(5)
print 'listening...'
conn,addr = serv.accept() #accept the connection
print '...connected!'
mouse = file('/dev/input/mouse0')
while True:
status, dx, dy = tuple(ord(c) for c in mouse.read(3))
def to_signed(n):
return n - ((0x80 & n) << 1)
dx = to_signed(dx)
dy = to_signed(dy)
conn.send(status)
conn.close()
client:
##client.py
from socket import *
HOST = 'localhost'
PORT = 29876 #our port from before
ADDR = (HOST,PORT)
BUFSIZE = 4096
cli = socket( AF_INET,SOCK_STREAM)
cli.connect((ADDR))
data = cli.recv(BUFSIZE)
while data != '':
print data
cli.close()
structmodule. If you want to continuously send data from some device, the common approach is to first let the client request that. Then the server starts sending the data.