2025年codependent(codependent 翻译)

codependent(codependent 翻译)简单并发实例 服务端 客户端 聊天并发实例 kehuduan 命令传送 1 conclusion nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp sendall 会把数据直接全部发送到客户端 客户端将所有的数据都放到缓冲区 每次 recv 多少字节取决于 recv 内的参数 理论不应该超过 8k 所以 并不能一次 recv 无限大数据 所以这里我们应该通过循环去接收 命令传送 2 解决大数据传送和粘包问题 View Code



简单并发实例

服务端:

客户端:

聊天并发实例

kehuduan:




命令传送1:

conclusion:

  sendall会把数据直接全部发送到客户端,客户端将所有的数据都放到缓冲区,每次recv多少字节取决于recv内的参数,理论不应该超过8k。

所以,并不能一次recv()无限大数据,所以这里我们应该通过循环去接收。

命令传送2:解决大数据传送和粘包问题

runcode 运行python_服务端

runcode 运行python_数据_02

View Code

runcode 运行python_服务端

runcode 运行python_数据_02

文件上传

注意:

   1 纸条就是conn

2 一收一发

1024)

if 那边send一个空数据 这边recv为空,则recv继续阻塞,等待其他的数据。所以聊天的时候好好聊,别发空数据。




虽说用Python编写简单的网络程序很方便,但复杂一点的网络程序还是用现成的框架比较好。这样就可以专心事务逻辑,而不是套接字的各种细节。SocketServer模块简化了编写网络服务程序的任务。同时SocketServer模块也是Python标准库中很多服务器框架的基础。

socketserver模块可以简化网络服务器的编写,Python把网络服务抽象成两个主要的类,一个是Server类,用于处理连接相关的网络操作,另外一个则是RequestHandler类,用于处理数据相关的操作。并且提供两个MixIn 类,用于扩展 Server,实现多进程或多线程。

Server类

它包含了种五种server类,BaseServer(不直接对外服务)。TCPServer使用TCP协议,UDPServer使用UDP协议,还有两个不常使用的,即UnixStreamServer和UnixDatagramServer,这两个类仅仅在unix环境下有用(AF_unix)。

Base class for server classes.

This uses the Internet TCP protocol, which provides for continuous streams of data between the client and server.

This uses datagrams, which are discrete packets of information that may arrive out of order or be lost while in transit. The parameters are the same as for TCPServer

These more infrequently used classes are similar to the TCP and UDP classes, but use Unix domain sockets; they’re not available on non-Unix platforms. The parameters are the same as for TCPServer. 

BaseServer的源码:

runcode 运行python_服务端

runcode 运行python_数据_02

View Code

There are five classes in an inheritance diagram, four of which represent synchronous servers of four types:

RequestHandler类

所有requestHandler都继承BaseRequestHandler基类。

runcode 运行python_服务端

runcode 运行python_数据_02

View Code

创建一个socketserver 至少分以下几步

  1. First, you must create a request handler class by subclassing the BaseRequestHandlerclass and overriding its handle() method; this method will process incoming requests.   
  2. Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
  3. Then call the handle_request() orserve_forever() method of the server object to process one or many requests.
  4. Finally, call server_close() to close the socket.
  1. View Code

让你的socketserver并发起来, 必须选择使用以下一个多并发的类

所以:

思考:

1 tcp与udp的区别(三次握手后,建立连接,双向通道,一个收,一个发,tcp每次接到数据后都会有一个应答,有了应答,新的数据就会被覆盖掉)

2 粘包  



编程小号
上一篇 2025-01-23 17:33
下一篇 2025-03-05 09:11

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/29682.html