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

歡迎來訪張軍網站:連續登錄【5】天博主自動為您充值【5】積分哦

獲取與端點的連接

系統 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條評論
主站蜘蛛池模板: 色姑娘久 | 精品伊人久久大香线蕉网站 | 精品亚洲在线 | 欧美伊人久久大香线蕉综合69 | 久久亚洲精品国产精品婷婷 | 小h片在线播放 | 日本sese | 久久婷婷国产综合精品 | 日本欧美高清全视频 | 欧美日韩精品一区二区三区四区 | 中文字幕视频在线播放 | 国产91亚洲精品 | 亚洲精品久久玖玖玖玖 | 97夜夜操 | 精品视自拍视频在线观看 | www.不卡视频| 欧美性白人顶级hd | 一级毛片真人不卡免费播 | 久久精品视频免费观看 | 精品国产一二三区在线影院 | 欧美视频在线观看免费 | 五月婷婷综合在线视频 | 国产91精品一区二区视色 | 成人亚洲性情网站www在线观看 | 中国日韩欧美中文日韩欧美色 | 五月激情五月婷婷 | 成人特级毛片69免费观看 | 亚洲精品国产一区二区三 | 国产色产综合色产在线观看视频 | 青青热久久久久综合精品 | 国产一区二区免费在线 | 亚洲精品乱码中文字幕无线 | 国产精品国产 | 亚洲国产精品日韩在线 | 一级二级三级毛片 | 日韩精品一区二区三区高清 | 中文字幕亚洲综合久久202 | 香蕉在线精品一区二区 | 天天干天天草天天射 | 性色黄 | 天天做夜夜操 |