原創博文,轉載請注明 出處 。
API 文檔: 點擊
? ? ? 在Twisted中,我們使用高級抽象的接口在傳輸和接收數據,比如
ITransport
和
IProtocol
。同時,Twisted也提供了構建面向數據流連接的端點的接口:
?
IStreamServerEndpoint
?and
IStreamClientEndpoint
。 “數據流”的意思是指端點連接是一個連續流的數據傳輸而不是一個序列的離散數據報:
?TCP is a "stream" protocol whereas UDP is a "datagram" protocol。
?創建和使用Endpoints : ??
? ? 通過前幾節我們知道,編寫一個客戶端或者服務器,我們通常是指定了地址和端口。但是有的時候在無需調整我們的程序的前提下,我們希望允許用戶指定監聽和連接的地址,允許用戶請求不同的策略,這時候我們就需要使用
clientFromString
?or?
serverFromString
。
每種類型的端點不過就是一個接口和一個需要參數的簡單方法。serverEndpoint.listen(factory)在你的protocol factory下 監聽端點,clientEndpoint,connect(factory)在啟動一個連接請求。
Servers and Stopping:
? ? ?
IStreamServerEndpoint.listen
?返回一個?由
IListeningPort
引發
?
Deferred
?。注意這個Deferred可能回調一個errback,最常見的情況就是有另外的程序在占用所請求的端口,實際情況因人而異。
如果你收到這樣的一個錯誤,這意味著您的應用程序實際上是沒有監聽,將不會得到任何傳入的連接。在這種情況下,它是提醒管理員的服務器出現了錯誤,尤其是如果你只有一個監聽端口。
? ? ? ?還有,如果連接一旦成功,它將永遠監聽,如果你需要關閉,除了對服務器全部關閉的方法(reactor.stop()),確保你對監聽端口對象保留一個引用,你可以調用 IListeningPort.stopListening ,最后,記住stopListening 本身返回一個deferred,直到deferred被觸發,對于端口的監聽可能沒有完全停止。
? ? ? ?大多數服務器應用程序不必擔心這些細節。
Clients and Cancelling:
? ? ?
connectProtocol
?把一個?
Protocol
?實例 連接到給定的 >
IStreamClientEndpoint
。連接一旦建立,它返回一個由protocol 觸發的deferred。通過
?
client documentation
?我們可以看到應用的例子.
? ? ?
connectProtocol
?是一個包裝了低級 API:?
IStreamClientEndpoint.connect
的包裝器,它為嘗試一個外部連接使用 protocol factory。其返回一個deferred?
?,deferred伴隨著一個由
factory's?
buildProtocol
?方法或者連接失敗產生的errbacks返回的IProtocol 所觸發。?
? ? ?有的時候連接耗費了很長時間,你的用戶可能因此而感覺煩惱。如果需要,你可以調用
?
Deferred.cancel
來放棄連接。這應該會引起deferred的errback,通常是
CancelledError
。盡管一些端點提供了內置的超時,但是該接口并不能保證所有的都擁有。為此我們可以自己構造一個方法來取消一個永遠保持等待的連接嘗試。
下面是一個非常簡單的30秒超時:?
attempt =
connectProtocol(myEndpoint, myProtocol)
reactor.callLater(
30, attempt.cancel)
注意,如果你之前用過clientFactory,記住endpoint.connect()使用Factory而不是clientFactory。如果你傳遞一個clientFactory給connec方法,將會調用
clientConnectionFailed
?and?
clientConnectionLos。
使用 Endpoint的好處 :
? ?待理解。
? ? 有的時候,你也不必需要使用Endpoint,如果你只是綁定一個簡單的端口, 你只需要使用構造
IService
,(使用?
strports.service
) 這很符合
the twistd plugin
API
的框架。這種方法提供了與Endpoint同樣的靈活性。
?
? Endpoint Types Included With Twisted:
? ?
clientFromString
?and?
serverFromString 所使用的解釋器可以通過第三方
plugins來擴展。所以endpoints的可用與否取決于你的系統中所安裝的包,但是Twisted本身包含了一組基本的可用端點。
??
Clients
-
TCP. Supported arguments: host, port, timeout. timeout is optional. For example,?
tcp:host=twistedmatrix.com:port=80:timeout=15
. -
SSL. All TCP arguments are supported, plus: certKey, privateKey, caCertsDir. certKey (optional) gives a filesystem path to a certificate (PEM format). privateKey (optional) gives a filesystem path to a a private key (PEM format). caCertsDir (optional) gives a filesystem path to a directory containing trusted CA certificates to use to verify the server certificate. For example,
ssl:host=twistedmatrix.com:port=443:caCertsDir=/etc/ssl/certs
. -
UNIX. Supported arguments: path, timeout, checkPID. path gives a filesystem path to a listening UNIX domain socket server. checkPID (optional) enables a check of the lock file Twisted-based UNIX domain socket servers use to prove they are still running. For example,
unix:path=/var/run/web.sock
.
Servers
-
TCP (IPv4). Supported arguments: port, interface, backlog. interface and backlog are optional. interface is an IP address (belonging to the IPv4 address family) to bind to. For example,?
tcp:port=80:interface=192.168.1.1
. -
TCP (IPv6). All TCP arguments are supported, with interface taking an IPv6 address literal instead. For example,
tcp6:port=80:interface=2001\:0DB8\:f00e\:eb00\:\:1
. -
SSL. All TCP arguments are supported, plus: certKey, privateKey, and sslmethod. certKey (optional, defaults to the value of privateKey) gives a filesystem path to a certificate (PEM format). privateKey gives a filesystem path to a a private key (PEM format). sslmethod indicates which SSL/TLS version to use (a value like TLSv1_METHOD). For example,?
ssl:port=443:privateKey=/etc/ssl/server.pem:sslmethod=SSLv3_METHOD
. -
UNIX. Supported arguments: address, mode, backlog, lockfile. address gives a filesystem path to listen on with a UNIX domain socket server. mode (optional) gives the filesystem permission/mode (in octal) to apply to that socket. lockfile enables use of a separate lock file to prove the server is still running. For example,?
unix:address=/var/run/web.sock:lockfile=1
.
-
systemd. Supported arguments: domain, index. domain indicates which socket domain the inherited file descriptor belongs to (eg INET, INET6). index indicates an offset into the array of file descriptors which have been inherited from systemd. For example,?
systemd:domain=INET6:index=3
. See also? Deploying Twisted with systemd .
?
?
? ?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
