我试图连接我的代码,这些代码是用python和processing(java)编写的。 python挂起,直到它获得一些数据。这给我带来了一些问题!我使用select.select但它再次挂起,直到它获得一些数据。如果你帮助我,我很高兴。
这是我的python套接字类:
import socket
import select
import time
class client():
def __init__(self,HOST,PORT):
self.HOST = HOST # Symbolic name meaning all available interfaces
self.PORT = PORT # Arbitrary non-privileged port
self.connect()
def connect(self):
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.bind((self.HOST, self.PORT))
self.s.listen(1)
self.conn, self.addr = self.s.accept()
print('Connected by', self.addr)
return
def readData(self):
do_read = False
try:
r, _, _ = select.select([self.conn], [], [])
do_read = bool(r)
except socket.error:
pass
if do_read:
# ready to receive
data = self.conn.recv(1024)
return data
return 0
print('hello')
x=client('localhost',5204);
#print('Connected by', addr)
while True:
data=x.readData();
if data:
print(data)