Python中asyncore異步模塊的用法及實現(xiàn)httpclient的實例
基礎(chǔ)
這個模塊是socket的異步實現(xiàn),讓我們先來熟悉一下模塊中的一些類和方法:
1.asyncore.loop
輸入一個輪詢循環(huán)直到通過計數(shù)或打開的通道已關(guān)閉。
2.asyncore.dispatcher
dispatcher類是一個底層socket類的包裝對象。要使它更有用, 它有一部分事件處理方法被異步循環(huán)調(diào)用。否則它就是一個標準的非阻塞socket對象。
底層的事件在特定事件或特定的連接狀態(tài)告訴異步循環(huán),某些高級事件發(fā)生了。例如, 我們要求一個socket連接到另一個主機。
(1)handle_connect() 第一次讀或?qū)懯录?br />
(2)handle_close() 讀事件沒有數(shù)據(jù)可用。
(3)handle_accept 讀事件監(jiān)聽一個socket。
(4)handle_read
在異步循環(huán)察覺到通道呼叫read()時調(diào)用。
(5)handle_write
在異步循環(huán)檢測到一個可寫的socket可以寫的時候調(diào)用。這種方法經(jīng)常實現(xiàn)緩沖性能。比如
def handle_write(self): sent = self.send(self.buffer) self.buffer = self.buffer[sent:]
(6)handle_expt
當有(OOB)數(shù)據(jù)套接字連接。這幾乎永遠不會發(fā)生,因為OOB精細地支持和很少使用。
(7)handle_connect
當socket創(chuàng)建一個連接時調(diào)用。
(8)handle_close
當socket連接關(guān)閉時調(diào)用。
(9)handle_error
當引發(fā)一個異常并沒有其他處理時調(diào)用。
(10)handle_accept
當本地監(jiān)聽通道與遠程端建立連接(被動連接)時調(diào)用。
(11)readable
每次在異步循環(huán)確定是否添加一個通道socket到讀事件列表時調(diào)用,默認都為True。
(12)writable
每次在異步循環(huán)確定是否添加一個通道socket到寫事件列表時調(diào)用, 默認為True。
(13)create_socket
與創(chuàng)建標準socket的時候相同。
(14)connect
與標準socket的端口設(shè)置是相同, 接受一個元組第一個參數(shù)為主機地址,第二個參數(shù)是端口號。
(15)send
向遠程端socket發(fā)送數(shù)據(jù)。
(16)recv
從遠程端socket讀取最多buffer_size的數(shù)據(jù)。一個空的字符串意味著從另一端通道已關(guān)閉。
(17)listen
監(jiān)聽socket連接。
(18)bind
將socket綁定到地址。
(19)accept
接受一個連接, 必須綁定到一個socket和監(jiān)聽地址。
(20)close
關(guān)閉socket。
3.asyncore.dispatcher_with_send
dispatcher子類添加了簡單的緩沖輸出功能用于簡單的客戶,更復(fù)雜的使用asynchat.async_chat。
4.asyncore.file_dispatcher
file_dispatcher需要一個文件描述符或文件對象地圖以及一個可選的參數(shù),包裝,使用調(diào)查()或循環(huán)()函數(shù)。如果提供一個文件對象或任何fileno()方法,該方法將調(diào)用和傳遞到file_wrapper構(gòu)造函數(shù)??捎眯?UNIX。
5.asyncore.file_wrapper
file_wrapper需要一個整數(shù)文件描述符并調(diào)用os.dup()復(fù)制處理,這樣原來的處理可能是獨立于file_wrapper關(guān)閉。這個類實現(xiàn)足夠的方法來模擬一個套接字使用file_dispatcher類??捎眯?UNIX。
asyncore 實例
1.一個http client的實現(xiàn)。
import socket
import asyncore
class Client(asyncore.dispatcher):
def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, 80))
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
def handle_connect(self):
pass
def handle_close(self):
self.close()
def handle_read(self):
print self.recv(8192)
def writable(self):
return (len(self.buffer) > 0)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
client = Client('www.python.org', '/')
asyncore.loop()
服務(wù)器接受連接和分配任務(wù)
import socket
import asyncore
class EchoHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(8192)
if data:
self.send(data)
class EchoServer(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_add()
self.bind((host, port))
self.listen(5)
def handle_accept(self):
pair = self.accept()
if pair is not None:
sock, addr = pair
print 'Incoming connection from %s' % repr(addr)
handler = EchoHandler(sock)
server = EchoServer('localhost', 8080)
asyncore.loop()
2.利用asyncore的端口映射(端口轉(zhuǎn)發(fā))
import socket,asyncore
class forwarder(asyncore.dispatcher):
def __init__(self, ip, port, remoteip,remoteport,backlog=5):
asyncore.dispatcher.__init__(self)
self.remoteip=remoteip
self.remoteport=remoteport
self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((ip,port))
self.listen(backlog)
def handle_accept(self):
conn, addr = self.accept()
# print '--- Connect --- '
sender(receiver(conn),self.remoteip,self.remoteport)
class receiver(asyncore.dispatcher):
def __init__(self,conn):
asyncore.dispatcher.__init__(self,conn)
self.from_remote_buffer=''
self.to_remote_buffer=''
self.sender=None
def handle_connect(self):
pass
def handle_read(self):
read = self.recv(4096)
# print '%04i -->'%len(read)
self.from_remote_buffer += read
def writable(self):
return (len(self.to_remote_buffer) > 0)
def handle_write(self):
sent = self.send(self.to_remote_buffer)
# print '%04i <--'%sent
self.to_remote_buffer = self.to_remote_buffer[sent:]
def handle_close(self):
self.close()
if self.sender:
self.sender.close()
class sender(asyncore.dispatcher):
def __init__(self, receiver, remoteaddr,remoteport):
asyncore.dispatcher.__init__(self)
self.receiver=receiver
receiver.sender=self
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((remoteaddr, remoteport))
def handle_connect(self):
pass
def handle_read(self):
read = self.recv(4096)
# print '<-- %04i'%len(read)
self.receiver.to_remote_buffer += read
def writable(self):
return (len(self.receiver.from_remote_buffer) > 0)
def handle_write(self):
sent = self.send(self.receiver.from_remote_buffer)
# print '--> %04i'%sent
self.receiver.from_remote_buffer = self.receiver.from_remote_buffer[sent:]
def handle_close(self):
self.close()
self.receiver.close()
if __name__=='__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option(
'-l','--local-ip',
dest='local_ip',default='127.0.0.1',
help='Local IP address to bind to')
parser.add_option(
'-p','--local-port',
type='int',dest='local_port',default=80,
help='Local port to bind to')
parser.add_option(
'-r','--remote-ip',dest='remote_ip',
help='Local IP address to bind to')
parser.add_option(
'-P','--remote-port',
type='int',dest='remote_port',default=80,
help='Remote port to bind to')
options, args = parser.parse_args()
forwarder(options.local_ip,options.local_port,options.remote_ip,options.remote_port)
asyncore.loop()
相關(guān)文章
python中關(guān)于os.path.pardir的一些坑
這篇文章主要介紹了python中關(guān)于os.path.pardir的一些坑及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
Python?Pandas讀取csv/tsv文件(read_csv,read_table)的區(qū)別
這篇文章主要給大家介紹了關(guān)于Python?Pandas讀取csv/tsv文件(read_csv,read_table)區(qū)別的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家學習或者使用Pandas具有一定的參考學習價值,需要的朋友可以參考下2022-01-01
詳解Django+Uwsgi+Nginx的生產(chǎn)環(huán)境部署
這篇文章主要介紹了Django + Uwsgi + Nginx 的生產(chǎn)環(huán)境部署,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

