python 編程之twisted詳解及簡單實(shí)例
python 編程之twisted詳解
前言:
我不擅長寫socket代碼。一是用c寫起來比較麻煩,二是自己平時(shí)也沒有這方面的需求。等到自己真正想了解的時(shí)候,才發(fā)現(xiàn)自己在這方面確實(shí)有需要改進(jìn)的地方。最近由于項(xiàng)目的原因需要寫一些Python代碼,才發(fā)現(xiàn)在python下面開發(fā)socket是一件多么爽的事情。
對于大多數(shù)socket來說,用戶其實(shí)只要關(guān)注三個事件就可以了。這分別是創(chuàng)建、刪除、和收發(fā)數(shù)據(jù)。python中的twisted庫正好可以幫助我們完成這么一個目標(biāo),實(shí)用起來也不麻煩。下面的代碼來自twistedmatrix網(wǎng)站,我覺得挺不錯的,貼在這里和大家分享一下。如果需要測試的話,直接telnet localhost 8123就可以了。如果需要在twisted中處理信號,可以先注冊signal函數(shù),在signal函數(shù)中調(diào)用reactor.stop(),后面twisted繼續(xù)call stop_factory,這樣就可以繼續(xù)完成剩下的清理工作了。
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class Chat(LineReceiver):
def __init__(self, users):
self.users = users
self.name = None
self.state = "GETNAME"
def connectionMade(self):
self.sendLine("What's your name?")
def connectionLost(self, reason):
if self.name in self.users:
del self.users[self.name]
def lineReceived(self, line):
if self.state == "GETNAME":
self.handle_GETNAME(line)
else:
self.handle_CHAT(line)
def handle_GETNAME(self, name):
if name in self.users:
self.sendLine("Name taken, please choose another.")
return
self.sendLine("Welcome, %s!" % (name,))
self.name = name
self.users[name] = self
self.state = "CHAT"
def handle_CHAT(self, message):
message = "<%s> %s" % (self.name, message)
for name, protocol in self.users.iteritems():
if protocol != self:
protocol.sendLine(message)
class ChatFactory(Factory):
def __init__(self):
self.users = {} # maps user names to Chat instances
def buildProtocol(self, addr):
return Chat(self.users)
def startFactory(self):
print 'start'
def stopFactory(self):
print 'stop'
reactor.listenTCP(8123, ChatFactory())
reactor.run()
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- python如何通過twisted搭建socket服務(wù)
- Python3.6中Twisted模塊安裝的問題與解決
- python安裝twisted的問題解析
- python如何通過twisted實(shí)現(xiàn)數(shù)據(jù)庫異步插入
- python基于twisted框架編寫簡單聊天室
- Python 基于Twisted框架的文件夾網(wǎng)絡(luò)傳輸源碼
- 剖析Python的Twisted框架的核心特性
- 實(shí)例解析Python的Twisted框架中Deferred對象的用法
- 詳解Python的Twisted框架中reactor事件管理器的用法
- 使用Python的Twisted框架編寫非阻塞程序的代碼示例
- Python的Twisted框架中使用Deferred對象來管理回調(diào)函數(shù)
- 使用Python的Twisted框架構(gòu)建非阻塞下載程序的實(shí)例教程
- Python的Twisted框架上手前所必須了解的異步編程思想
- 使用Python的Treq on Twisted來進(jìn)行HTTP壓力測試
- 利用Python的Twisted框架實(shí)現(xiàn)webshell密碼掃描器的教程
- 使用Python的Twisted框架實(shí)現(xiàn)一個簡單的服務(wù)器
- 使用Python的Twisted框架編寫簡單的網(wǎng)絡(luò)客戶端
- python開發(fā)實(shí)例之Python的Twisted框架中Deferred對象的詳細(xì)用法與實(shí)例
相關(guān)文章
Python機(jī)器學(xué)習(xí)性能度量利用鳶尾花數(shù)據(jù)繪制P-R曲線
這篇文章主要為大家介紹了Python機(jī)器學(xué)習(xí)性能度量利用鳶尾花數(shù)據(jù)繪制P-R曲線示例學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
Flask框架運(yùn)用Ajax實(shí)現(xiàn)數(shù)據(jù)交互的示例代碼
使用Ajax技術(shù)網(wǎng)頁應(yīng)用能夠快速地將增量更新呈現(xiàn)在用戶界面上,而不需要重載刷新整個頁面,這使得程序能夠更快地回應(yīng)用戶的操作,本文將簡單介紹使用AJAX如何實(shí)現(xiàn)前后端數(shù)據(jù)通信2022-11-11
Python OpenCV處理圖像之濾鏡和圖像運(yùn)算
這篇文章主要為大家詳細(xì)介紹了Python OpenCV處理圖像之濾鏡和圖像運(yùn)算,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
django 實(shí)現(xiàn)手動存儲文件到model的FileField
這篇文章主要介紹了django 實(shí)現(xiàn)手動存儲文件到model的FileField,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

