简体   繁体   English

读取所有套接字异步

[英]Read all socket asyncio

Server await 1024 bytes:服务器等待 1024 字节:

import asyncio


async def handle_client(reader, writer):
    data = await reader.read(1024)
    writer.write("hello from server".encode())
    await writer.drain()
    writer.close()


loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_server(handle_client, 'localhost', 4782))
loop.run_forever()

Client Gen 2048 bytes and send on server:客户端生成 2048 字节并在服务器上发送:

import socket
sock = socket.socket()
sock.connect(('localhost', 4782))
sock.sendall(("".join([str(i)[-1:] for i in range(2048)])).encode())
data = sock.recv(1024)
sock.close()

trouble: if buff=1020 Client sent 3000bytes server stop client and freeze him麻烦:如果 buff=1020 客户端发送 3000 字节服务器停止客户端并冻结他

This question is something that I think a lot of beginners get confused because they don't fully understand how to read sockets until completion.这个问题我认为很多初学者会感到困惑,因为他们在完成之前并不完全理解如何阅读 sockets。 The answer is simple, but likely unsatisfactory:答案很简单,但可能并不令人满意:

It depends on the protocol you are using.这取决于您使用的协议。

If you look at a standard like HTTP/1.1 the way client and servers know when the message is done (simplified for explanation - there is more to it, but for the general gist) is via the Content-Length header and a double \r\n escape characters.如果您查看 HTTP/1.1 之类的标准,客户端和服务器知道消息何时完成的方式(为解释而简化 - 还有更多内容,但对于一般要点)是通过Content-Length header 和双\r\n转义字符。 So when the client sends something over to the server, he/she would send:所以当客户端向服务器发送一些东西时,他/她会发送:

GET / HTTP/1.1\r\n
Host: www.example.com\r\n
Connection: close\r\n\r\n

In this case the client is requesting the root directory / via a GET request using the HTTP/1.1 protocol.在这种情况下,客户端使用HTTP/1.1协议通过GET请求请求根目录/

Notice the double \r\n - that is the completion of the header and message, and the server will not read the socket beyond this since there is no Content-Length header present.注意双\r\n - 即 header 和消息的完成,并且服务器不会读取超出此范围的套接字,因为没有Content-Length header 存在。 Then the server would reply with:然后服务器会回复:

HTTP/1.1 200 OK\r\n
Content-Length: 18\r\n\r\n
{"hello": "world"}

This a dumb-down explanation how both client and server know how long to read a socket for when using HTTP/1.1.这是一个愚蠢的解释,客户端和服务器在使用 HTTP/1.1 时如何知道读取套接字的时间。 You can easily find more information about the protocol online, and there are a few libraries out there that will help you creating your own raw HTTP requests.您可以轻松地在线找到有关该协议的更多信息,并且有一些库可以帮助您创建自己的原始 HTTP 请求。

If you want to write your own version of this - I would not recommend to use your own protocol in production unless you really know what you are doing - you could be a bit more simple and simply read the socket until a symbol (example) \0 is reached, which indicates the end of either the client or server message.如果您想编写自己的版本-除非您真的知道自己在做什么,否则我不建议您在生产中使用自己的协议-您可以更简单一些,只需读取套接字直到出现符号(示例) \0到达\0 ,表示客户端或服务器消息的结束。

In short: the protocol matters, and if you are not using a standard protocol you need to figure out how to tell the client and server until when they should read their socket for.简而言之:协议很重要,如果你没有使用标准协议,你需要弄清楚如何告诉客户端和服务器,直到他们应该读取他们的套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM