Socketserver详解
+------------+
|
v
+-----------+ +------------------+
| TCPServer |------->| UnixStreamServer |
+-----------+ +------------------+
|
v
+-----------+ +--------------------+
| UDPServer |------->| UnixDatagramServer |
- First, you must create a request handler class by subclassing the
BaseRequestHandlerclass and overriding itshandle()method; this method will process incoming requests. - Second, you must instantiate one of the server classes, passing it the server’s address and the request handler class.
- Then call the
handle_request()orserve_forever()method of the server object to process one or many requests. - Finally, call
server_close()to close the socket.
BaseRequestHandlerclass ,并且还要重写父类里handle()方法;import socketserver
class MyServer(socketserver.BaseRequestHandler):
def handle(self):
# 创建一个链接,继承于socketserver中的BaseRequestHandler类
conn = self.request
# 发送登录提示
conn.sendall(b"Welcome to login...")
print("Client connect...")
while True:
print("Waitting for recving message...")
# 接收消息
message = conn.recv()
print(message.decode('utf-8'))
# 收到exit就退出
if message == "exit":
break
# 回复消息
data = input("Reply message:")
# 发送消息
conn.sendall(data.encode('utf-8'))
if __name__ == "__main__":
# 实例化
server = socketserver.ThreadingTCPServer(('127.0.0.1', , ), MyServer)
# 调用serve_forever方法
server.serve_forever()
'''
def serve_forever(self, poll_interval=0.5):
"""
Handle one request at a time until shutdown.
Polls for shutdown every poll_interval seconds. Ignores
self.timeout. If you need to do periodic tasks, do them in
another thread.
"""
'''
multi_socketserver_client.py # 就是一个简单的TCP客户端
import socket
sock = socket.socket()
# 连接服务端
sock.connect(('127.0.0.1', , ))
login = sock.recv()
print(login.decode('utf-8'))
while True:
message = input("Please input the message:").strip()
if message == "exit":
sock.sendall(b'exit')
break
else:
sock.sendall(message.encode('utf-8'))
print("Waitting for recving message...")
data = sock.recv()
print(data.decode('utf-8'))
sock.close()
Socketserver详解的更多相关文章
- kafka启动时出现FATAL Fatal error during KafkaServer startup. Prepare to shutdown (kafka.server.KafkaServer) java.io.IOException: Permission denied错误解决办法(图文详解)
首先,说明,我kafk的server.properties是 kafka的server.properties配置文件参考示范(图文详解)(多种方式) 问题详情 然后,我启动时,出现如下 [hadoop ...
- kafka_2.11-0.8.2.2.tgz的3节点集群的下载、安装和配置(图文详解)
kafka_2.10-0.8.1.1.tgz的1或3节点集群的下载.安装和配置(图文详细教程)绝对干货 一.安装前准备 1.1 示例机器 二. JDK7 安装 1.1 下载地址 下载地址: http: ...
- Ubuntu16.04下沙盒数据导入到 Neo4j 数据库(图文详解)
不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793914 (表示感谢) 前期博客 Neo4j沙盒实验申请过 ...
- Linq之旅:Linq入门详解(Linq to Objects)
示例代码下载:Linq之旅:Linq入门详解(Linq to Objects) 本博文详细介绍 .NET 3.5 中引入的重要功能:Language Integrated Query(LINQ,语言集 ...
- 架构设计:远程调用服务架构设计及zookeeper技术详解(下篇)
一.下篇开头的废话 终于开写下篇了,这也是我写远程调用框架的第三篇文章,前两篇都被博客园作为[编辑推荐]的文章,很兴奋哦,嘿嘿~~~~,本人是个很臭美的人,一定得要截图为证: 今天是2014年的第一天 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- Java 字符串格式化详解
Java 字符串格式化详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 文中如有纰漏,欢迎大家留言指出. 在 Java 的 String 类中,可以使用 format() 方法 ...
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
随机推荐
- 利用wsdl.exe生成webservice代理类
通常要手动生成WebService代理类需要把一句生成语句,如 wsdl.exe /l:cs /out:D:\Proxy_UpdateService.cs http://localhost:1101 ...
- 利用Vagrant and VirtualBox搭建core os环境
利用Vagrant and VirtualBox搭建core os环境 系统环境 ubuntu 14.04 x64 vagrant 1.7.4 virtualbox 4.3.10 git 1.9.1 ...
- Flat UI theme--扁平化的UI
项目地址:点击打开 支持版本: jQuery Mobile 1.3.2 使用很简单,前提是你的前端是在jquery-mobile的基础上开发的,然后导入相应的css文件.img文件和js文件即可. 案 ...
- Kendo MVVM 数据绑定(三) Click
Kendo MVVM 数据绑定(三) Click Click 绑定可以把由 ViewModel 定义的方法不绑定到目标 DOM 的 click 事件.当点击目标 DOM 元素时触发 ViewModel ...
- HttpServeletRequest
一.HttpServeletRequest 接口(javax.servlet.http) 定义:public interface HttpServletRequestextends ServletRe ...
- To run dex in process, the Gradle daemon needs a larger heap
http://blog.csdn.net/u012995856/article/details/52595653
- pt-table-checksum和pt-table-sync
环境:系统bsd,标准安装,ports安装的mysql. 主172.16.21.126 从172.16.21.128vi /etc/rc.conf 添加 mysql_enable="YES& ...
- 解决Layui的switch样式显示问题
Layui官方文档是这么说的: <input type="checkbox" name="xxx" lay-skin="switch" ...
- 【Python图像特征的音乐序列生成】如何标记照片的特征
目前我能想到的办法是这样的: 1,提取照片中的实体特征,借用某个pre-trained model进行tag标记. 2,将特征组合起来,形成一个bag-of-word model,然后将这个向量作为输 ...
- BeautifulSoup库测试代码
import requests from bs4 import BeautifulSoup import time headers={ #'User-Agent':'Nokia6600/1.0 (3. ...