Python network programming (about twisted)

Source: Internet
Author: User

Python network programming (about twisted)
I am not good at writing socket code. First, it is difficult to write data in c, and second, you do not have such requirements at ordinary times. When you really want to know about it, you will find that you need to improve it. Recently, some python code needs to be written due to project reasons, so it is quite nice to develop socket under python.

For most sockets, users only need to pay attention to three events. Create, delete, and send and receive data respectively. The twisted library in python can help us achieve this goal, and it is not difficult to use it. The following code is from the twistedmatrix website. I think it is quite good. I will share it with you here. If you need to test it, telnet localhost 8123.

from twisted.internet.protocol import Factoryfrom twisted.protocols.basic import LineReceiverfrom twisted.internet import reactorclass Chat(LineReceiver):    def __init__(self, users):        self.users = users        self.name = None        self.state = "GETNAME"    def connectionMade(self):        self.sendLine("What's your name?")    def connectionLost(self, reason):        if self.name in self.users:            del self.users[self.name]    def lineReceived(self, line):        if self.state == "GETNAME":            self.handle_GETNAME(line)        else:            self.handle_CHAT(line)    def handle_GETNAME(self, name):        if name in self.users:            self.sendLine("Name taken, please choose another.")            return        self.sendLine("Welcome, %s!" % (name,))        self.name = name        self.users[name] = self        self.state = "CHAT"    def handle_CHAT(self, message):        message = "<%s> %s" % (self.name, message)        for name, protocol in self.users.iteritems():            if protocol != self:                protocol.sendLine(message)class ChatFactory(Factory):    def __init__(self):        self.users = {} # maps user names to Chat instances    def buildProtocol(self, addr):        return Chat(self.users)reactor.listenTCP(8123, ChatFactory())reactor.run()

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.