Python socket TCPServer Demo
#coding:utf-8
from SocketServer import TCPServer,BaseRequestHandler
import traceback
class MyBaseRequestHandler(BaseRequestHandler):
"""
#继承BaseRequestHandler的handle方法
"""
def handle(self):
while True:
#当客户端主动断开连接时候,self.recv(1024)会抛出异常
try:
data = self.request.recv(1024).strip()
print "receive for(%r):%r"%(self.client_address,data)
self.request.sendall(data.upper())
except:
traceback.print_exc()
break
if __name__ == "__main__":
# 开启ip和端口
ip_port = ("192.168.1.103", 8090)
#构造TCPServer对象
server = TCPServer(ip_port,MyBaseRequestHandler)
#启动服务器监听
server.serve_forever()
文章对您有帮助,开心可以打赏我,金额随意,欢迎来赏!
需要电子方面开发板/传感器/模块等硬件可以到我的淘宝店逛逛