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 ...
随机推荐
- C#关键字:yield
yield是C#为了简化遍历操作实现的语法糖.在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.有两种形式: yield return <expres ...
- PHP正则表达式 - 附录(常用正则表达式)
常用正则表达式附录I 平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: "^\d+$" //非负整数(正整数 + 0) "^[0-9]*[1- ...
- Webstorm 激活
注册时,在打开的License Activation窗口中选择“License server”,在输入框输入下面的网址: http://idea.iteblog.com/key.php 点击:Acti ...
- swift基础-3
fallthrough 贯穿 case 可以不必写break 如果不需要知道区间内 每一项的值 可以使用 下划线 —— 来代替变量名 忽略 对该值的访问 for index in 1...5{ p ...
- 织梦修改文档HTML默认保存路径
\data\config.cache.inc.php $cfg_arcdir = '/a'; 改为 $cfg_arcdir = '/';
- SpringBoot LogBack文件配置
<?xml version="1.0" encoding="UTF-8"?> <configuration scan="true&q ...
- HTML页面右键事件
<script type="text/javascript"> <!-- document.onmousedown = function (e) { var e ...
- Android商城开发系列(五)—— 商城首页回到顶部和搜索框布局实现
今天我们来开发商城的首页[输入搜索框]布局和点击右下角图片回到顶部的效果 搜索功能在App中很常见,尤其是在商城类的项目当中,一般都会提供很强大的搜索功能,App的搜索布局一般都是在App的顶部,如下 ...
- Windows系统下查看文件编码类型
这是一个程序员的最基本的技能,原谅我到现在才去了解 以前只知道window操作系统下文件大部分默认编码是ANSI,中文版是GBK编码 如果想要查看或者修改文件编码的话有两种方式 一:用记事本打开文件, ...
- Codeforces Round #313 (Div. 2) A.B,C,D,E Currency System in Geraldion Gerald is into Art Gerald's Hexagon Equivalent Strings
A题,超级大水题,根据有没有1输出-1和1就行了.我沙茶,把%d写成了%n. B题,也水,两个矩形的长和宽分别加一下,剩下的两个取大的那个,看看是否框得下. C题,其实也很简单,题目保证了小三角形是正 ...