亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

獲取與端點的連接

系統 1680 0

原創博文,轉載請注明 出處

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元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 99热这里只有精品99 | 九九影院最新理论片 | 国产资源站| 四虎影院的网址 | 91国自产精品中文字幕亚洲 | 国产精品1 | 国产成人精品一区二区视频 | 久久综合免费视频 | 最新仑乱免费视频 | 亚欧成人在线 | 一级特黄国产高清毛片97看片 | 成人在线不卡 | 亚洲国产精品一区二区久久 | 亚洲精国产一区二区三区 | 奇米影视狠狠久久中文 | 亚洲国产成人久久99精品 | 国产精品19禁在线观看2021 | 四虎精品成人免费影视 | 精品久久久久久无码中文字幕 | 日韩欧美一卡二区 | 国产香蕉久久精品综合网 | 四虎影视4hu4虎成人 | 亚洲精品一二三区-久久 | 狠狠2020| 亚洲va久久久噜噜噜久久男同 | 欧美日韩午夜视频 | 精品国产福利在线观看一区 | 狠狠操狠狠操狠狠操 | 久久精品免费一区二区视 | 在线视频一二三区2021不卡 | 一级特黄aaa大片在 一级特黄aaa大片在线观看 | 精品久久久久久中文字幕女 | 日韩亚洲欧美在线观看 | 免费观看黄色录像 | 99久久精品免费观看区一 | 国产视频一区二区 | 成人在线不卡视频 | videosex久久麻豆 | 99re66热这里只有精品首页 | 欧美一级毛片片免费 | 色综合欧美综合天天综合 |