获取与端点的连接

原创博文,转载请注明出处

API 文档: 点击

      在Twisted中,我们使用高级抽象的接口在传输和接收数据,比如ITransportIProtocol。同时,Twisted也提供了构建面向数据流连接的端点的接口: IStreamServerEndpoint andIStreamClientEndpoint。 “数据流”的意思是指端点连接是一个连续流的数据传输而不是一个序列的离散数据报:

 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.

 

   

原文地址:https://www.cnblogs.com/tracylining/p/3338310.html