这篇文章主要介绍了python如何通过twisted搭建socket服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
服务端
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 21:41
# @Author : cxa
# @File : tsTservTW.py
# @Software: PyCharm
'''
Twisted Reactor时间戳TCP服务器
'''
from twisted.internet import protocol, reactor
from time import ctime
import msgpack
PORT = 21567
class TSServProtocol(protocol.Protocol):
def connectionMade(self):
'''
当客户端连接的时候会执行该方法
:return:
'''
clnt = self.clnt = self.transport.getPeer().host
print(f"...来自的{clnt}链接:")
def dataReceived(self, data):
'''
接收到客户端的数据
:param data:
:return:
'''
print(f"来自客户端:{msg