读取从StreamRequestHandler发送的数据写入 [英] Reading data send from StreamRequestHandler write

查看:151
本文介绍了读取从StreamRequestHandler发送的数据写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SocketServer.StreamRequestHandler服务器,该服务器调用self.rfile.readline()读取请求,然后调用self.wfile.write(data)发送一些数据:

I have a SocketServer.StreamRequestHandler server that calls self.rfile.readline() to read a request and then calls self.wfile.write(data) to send back some data:

class FileServerHandler(SocketServer.StreamRequestHandler):

    def handle(self):
            # self.rfile is a file-like oject created by the handler
            data = self.rfile.readline()
            if data == "msg":
                    self.wfile.write(someOtherData)

我希望我的客户端能够发送请求并从服务器接收"someOtherData":

I want my client to be able to send the request and receive the "someOtherData" from the server:

# Create a socket (SOCK_STREAM means a TCP socket)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.connect((HOST, PORT))
sock.send("msg")
print sock.recv(1024)
sock.close()

但是,当我尝试此操作时,客户端挂起.我要去哪里错了?还有必要知道套接字接收多少数据,或者是否有办法只接收服务器写入的所有数据?

But the client hangs when I try this. Where am I going wrong? Also is it necessary to know how much data the socket recv's or is there a way to just receive all the data the server writes?

推荐答案

在Jan回答旁边我想提及一下,如果您想接收准确的消息,则需要使用strip来删除您输入的'\ n'.字符串的结尾.

beside Jan answers I like to mention if you want to receive your exact message, you need to use strip to drop '\n' that you have put at the end of your string .

这篇关于读取从StreamRequestHandler发送的数据写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆