Create a twisted reactor tcp client

Source: Internet
Author: User

Unlike the socketserver tcp client, this example is not similar to all other clients. It is completely twisted.

Example 16.8 twisted reactor timestamp tcp client (tstclnttw. py)

Rewrite the familiar timestamp tcp client with twisted.

1 #!/usr/bin/env python
2
3 from twisted.internet import protocol, reactor
4
5   HOST=' localhost '
6   PORT=21567

8 class TSClntProtocol(protocol.Protocol):
9      def sendData(self):
data = raw_input('> ')
11            if data:
12                print '...sending %s...' % data
13                self.transport.write(data)
14            else:
15                self.transport.loseConnection()
16
17      def connectionMade(self):
18            self.sendData()
19
20      def dataReceived(self, data):
21            print data
22            self.sendData()
23
24  class TSClntFactory(protocol.ClientFactory):
25      protocol = TSClntProtocol
26      clientConnectionLost = clientConnectionFailed = \
27          lambda self, connector, reason: reactor.stop()
28
29  reactor.connectTCP(HOST, PORT, TSClntFactory())
30  reactor.run()

Line-by-line explanation

1 ~ 6 rows

Similar to all previous client programs, the twisted component is imported here.

8 ~ 22 rows

Like the server, we extend protocol and rewrite the same functions connectionmade () and datareceived (). These two functions are used in the same way as the server. We add our own function senddata () for calling when data needs to be sent.

Because we are a client, we need to initiate a conversation with the server. Once the connection is established, we first send a message, the server replies to this message, we display the received response on the screen, and then send other messages to the server.

This process continues until the user does not give any input, and the connection ends. At the end, instead of calling the write () function of the transport object to transmit data to the server, the loseconnection () function is called to close the socket. At this time, the client connectionlost () function of the factory will be called. At the same time, the reactor will be closed and the execution of the script will end. For some reason, when clientconnectionfailed () is called, the reactor is also disabled.

The last part of the script is to create a client factory, connect to the server, and then run reactor. Note that we instantiate the client factory here, instead of uploading it to the reactor as in the server. This is because, instead of waiting for the client to connect to the server, the server must create a new protocol object for each connection when there is a connection. We are only a client, so we only need to create a protocol object to connect to the server, and the server factory will create a protocol object to talk to us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.