python如何通過twisted搭建socket服務(wù)
這篇文章主要介紹了python如何通過twisted搭建socket服務(wù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
服務(wù)端
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 21:41
# @Author : cxa
# @File : tsTservTW.py
# @Software: PyCharm
'''
Twisted Reactor時(shí)間戳TCP服務(wù)器
'''
from twisted.internet import protocol, reactor
from time import ctime
import msgpack
PORT = 21567
class TSServProtocol(protocol.Protocol):
def connectionMade(self):
'''
當(dāng)客戶端連接的時(shí)候會(huì)執(zhí)行該方法
:return:
'''
clnt = self.clnt = self.transport.getPeer().host
print(f"...來自的{clnt}鏈接:")
def dataReceived(self, data):
'''
接收到客戶端的數(shù)據(jù)
:param data:
:return:
'''
print(f"來自客戶端:{msgpack.unpackb(data,encoding='utf')}")
data = f"{ctime()}:來自服務(wù)器:你好"
self.transport.write(msgpack.packb(data))
if __name__ == '__main__':
# 創(chuàng)建一個(gè)協(xié)議工廠,之所以稱之為工廠是因?yàn)椋看蔚玫揭粋€(gè)
# 接入連接時(shí),都能"制造"協(xié)議的一個(gè)實(shí)例。
factory = protocol.Factory()
factory.protocol = TSServProtocol
print("....等待鏈接..")
# 使用reactor安裝一個(gè)TCP監(jiān)聽器,檢查服務(wù)請(qǐng)求。
# 當(dāng)它接收到一個(gè)請(qǐng)求時(shí),就會(huì)創(chuàng)建一個(gè)TSServProtocol實(shí)例來處理那個(gè)客戶端的事務(wù)。
reactor.listenTCP(PORT, factory)
reactor.run()
客戶端
# -*- coding: utf-8 -*-
# @Time : 2018/9/19 21:57
# @Author : cxa
# @File : tsTclntTW.py
# @Software: PyCharm
'''
創(chuàng)建Twisted Reactor TCP客戶端
'''
from twisted.internet import protocol, reactor
import msgpack
HOST = 'localhost'
PORT = 21567
class TSClntProtocol(protocol.Protocol):
def sendData(self):
data = input('>')
if data:
print(f'...發(fā)送數(shù)據(jù) {data}')
self.transport.write(msgpack.packb(data))
else:
self.transport.loseConnection()
def connectionMade(self):
self.sendData()
def dataReceived(self, data):
print(msgpack.unpackb(data, encoding="utf8"))
self.sendData()
class TSClntFactory(protocol.ClientFactory):
protocol = TSClntProtocol
clientConnctionLost = clientConnctionFailed = lambda self, connector, reason: reactor.stop()
if __name__ == '__main__':
reactor.connectTCP(HOST, PORT, TSClntFactory())
reactor.run()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python3.6中Twisted模塊安裝的問題與解決
- python安裝twisted的問題解析
- python如何通過twisted實(shí)現(xiàn)數(shù)據(jù)庫異步插入
- python基于twisted框架編寫簡(jiǎn)單聊天室
- python 編程之twisted詳解及簡(jiǎn)單實(shí)例
- Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
- 剖析Python的Twisted框架的核心特性
- 實(shí)例解析Python的Twisted框架中Deferred對(duì)象的用法
- 詳解Python的Twisted框架中reactor事件管理器的用法
- 使用Python的Twisted框架編寫非阻塞程序的代碼示例
- Python的Twisted框架中使用Deferred對(duì)象來管理回調(diào)函數(shù)
- 使用Python的Twisted框架構(gòu)建非阻塞下載程序的實(shí)例教程
- Python的Twisted框架上手前所必須了解的異步編程思想
- 使用Python的Treq on Twisted來進(jìn)行HTTP壓力測(cè)試
- 利用Python的Twisted框架實(shí)現(xiàn)webshell密碼掃描器的教程
- 使用Python的Twisted框架實(shí)現(xiàn)一個(gè)簡(jiǎn)單的服務(wù)器
- 使用Python的Twisted框架編寫簡(jiǎn)單的網(wǎng)絡(luò)客戶端
- python開發(fā)實(shí)例之Python的Twisted框架中Deferred對(duì)象的詳細(xì)用法與實(shí)例
相關(guān)文章
簡(jiǎn)單的連接MySQL與Python的Bottle框架的方法
這篇文章主要介紹了簡(jiǎn)單的連接MySQL與Python的Bottle框架的方法,主要基于mysql-connector插件,需要的朋友可以參考下2015-04-04
簡(jiǎn)化Python的Django框架代碼的一些示例
這篇文章主要介紹了簡(jiǎn)化Python的Django框架代碼的一些示例,實(shí)際上文中只是抽取了一些Django中最基本的功能用于簡(jiǎn)化入門者的上手復(fù)雜度,下,需要的朋友可以參考下2015-04-04
Python3.9用pip安裝wordcloud庫失敗的解決過程
一般在命令行輸入pip install wordcloud 總會(huì)顯示安裝失敗,所以下面這篇文章主要給大家介紹了關(guān)于Python3.9用pip安裝wordcloud庫失敗的解決過程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
Python 20行簡(jiǎn)單實(shí)現(xiàn)有道在線翻譯的詳解
這篇文章主要介紹了Python實(shí)現(xiàn)有道在線翻譯的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python實(shí)現(xiàn)詞云圖詞頻統(tǒng)計(jì)
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)分析中的詞頻統(tǒng)計(jì)和詞云圖可視化,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下2022-12-12
Django中和時(shí)區(qū)相關(guān)的安全問題詳解
這篇文章主要給大家介紹了關(guān)于Django中和時(shí)區(qū)相關(guān)的安全問題的相關(guān)資料,需要的朋友可以參考下2020-10-10

