Python StreamRequestHandler
So i've got a socket server running the StreamRequestHandler class. The problem lies in writing back to the socket. MY client does not seem to receive the data开发者_运维百科.
class MyTCPHandler(SocketServer.StreamRequestHandler)
def handle(self):
while 1:
self.data = self.rfile.readline()
...process data and pref result...
self.wfile.write(result)
break
this seems straight forward, what am i missing. Is there a way to flush the send buffer, as the amount of data i send is very little?
If I understand it correctly, you have problems with sending data from the server to the client. I had the same problem. Turns out, wfile
and rfile
are of type BytesIO and take only binary data.
So if your result
is a normal string, you'll have to make it binary.
bstring = b"your string"
bstring = bytes("normal string", "utf8")
class MyTCPHandler(SocketServer.StreamRequestHandler)
def handle(self):
while 1:
self.data = self.rfile.readline()
if not self.data:
break
self.wfile.write(b"Got some data\n")
精彩评论