当前位置:网站首页 > Python编程 > 正文

服务器部署pyhon环境后怎么用(服务器部署python程序)



简单并发实例

服务端:

客户端:

聊天并发实例

kehuduan:




命令传送1:

conclusion:

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

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

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

runcode 运行python_客户端

runcode 运行python_服务端_02

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

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

创建一个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 粘包  

到此这篇服务器部署pyhon环境后怎么用(服务器部署python程序)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • python中将list中的字符串转换成数字(python list字符串转list)2025-05-01 09:54:04
  • pivot函数的作用(pivot函数 python)2025-05-01 09:54:04
  • ubuntu下安装qt(ubuntu下安装python教程)2025-05-01 09:54:04
  • vs怎么运行(vs怎么运行python代码)2025-05-01 09:54:04
  • py创建文件夹(python3创建文件夹)2025-05-01 09:54:04
  • vs怎么用python(vs怎么用scanf输入)2025-05-01 09:54:04
  • onnx模型部署 python(onnx模型部署到单片机)2025-05-01 09:54:04
  • python可以没有返回值吗(python中哪些函数没有返回值)2025-05-01 09:54:04
  • python list转String(python list转String)2025-05-01 09:54:04
  • python函数如何返回多个值(python函数返回多个值的方法(入门必读))2025-05-01 09:54:04
  • 全屏图片