我遇到了麻烦,并且有很多关于下面的套接字编程附加代码的问题(所有部分都取自并一起编写)我正在尝试将鼠标数据发送到客户端,但出现错误:
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
问题: 1.我们如何通过字符串以外的套接字发送数据,或者特别是没有 .send("...") 语句。连续更改数据?2.发送数据流时要注意什么?3.这里写的代码一团糟,很高兴能帮助我教一些代码意识
谢谢你
代码:服务器端:
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.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()