Twisted and simple examples of Python programming

Source: Internet
Author: User
Twisted of Python programming

Objective:

I'm not good at writing socket code. One is to write with C more trouble, and the other is that they do not have the needs of this aspect. Wait until you really want to know, only to find yourself in this area there is a need to improve the place. Recently, because of the project, I needed to write some Python code to find out how cool it was to develop a socket under Python.

For most sockets, the user can only focus on three events. This is to create, delete, and send and receive data, respectively. The twisted Library in Python just helps us accomplish such a goal, and it's not cumbersome to be practical. The following code from the Twistedmatrix website, I think very good, paste here and share with you. If you need to test it, telnet to localhost 8123 directly. If you need to process the signal in twisted, you can register the signal function, call Reactor.stop () in the signal function, and then twisted continue with calls Stop_factory, so you can continue with the rest of the cleanup work.

From Twisted.internet.protocol import Factory from twisted.protocols.basic import linereceiver from twisted.internet Import Reactor Class Chat (linereceiver): Def __init__ (self, users): self.users = Users self.name = None s     Elf.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 (sel F, line): if self.state = = "GETNAME": Self.handle_getname (line) Else:self.handle_CHAT (line) def h       Andle_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.use Rs.iteritems (): if protocol! = Self:protocol.sEndLine (Message) class Chatfactory (Factory): def __init__ (self): Self.users = {} # Maps user names to Chat ins  Tances def buildprotocol (self, addr): Return Chat (self.users) def startfactory (self): print ' Start ' def Stopfactory (self): print ' Stop ' reactor.listentcp (8123, Chatfactory ()) Reactor.run ()

Thank you for reading, hope to help everyone, thank you for the support of this site!

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.