Python chat room implementation method based on in-process communication

Source: Internet
Author: User

Python chat room implementation method based on in-process communication

This article describes how to implement a python chat room based on in-process communication. Share it with you for your reference. The details are as follows:

#!/usr/bin/env python# Added by <ctang@redhat.com>import sysimport osfrom multiprocessing import connectionADDR = ('', 9997)AUTH_KEY = '12345'class Server(object):  def __init__(self, username):    self.auth_key = AUTH_KEY    self.addr = ADDR    self.username = username    self.listener = connection.Listener(self.addr, authkey=self.auth_key)  def listen(self):    while True:      conn = self.listener.accept()      while True:        try:          request = conn.recv()          response = self.response(request)          conn.send(response)        except EOFError:          break      conn.close()  def reply(self):    message = raw_input("%s: " % self.username)    return message  def output_request(self, request):    sys.stdout.write('%s says: %s\n' % request)  def response(self, request):    self.output_request(request)    response = (self.username, self.reply())    return responseclass Client(object):  def __init__(self, username):    self.auth_key = AUTH_KEY    self.addr = ADDR    self.username = username    self.display_name = self.make_display_name(username)  def make_display_name(self, username):    return "%s: " % username  def connect(self):    self.conn = connection.Client(self.addr, authkey=self.auth_key)    while True:      message = raw_input(self.display_name)      self.send(message)      response = self.conn.recv()      self.output_response(response)   def send(self, message):    self.conn.send((self.username, message))  def output_response(self, response):    sys.stdout.write('%s says: %s\n' % response)def main():  mode = sys.argv[1]  if mode == 'server':    username = raw_input("Your name please: ")    server = Server(username)    server.listen()  elif mode == 'client':    username = raw_input("Your name please: ")    client = Client(username)    client.connect()if __name__ == '__main__':  main()

I hope this article will help you with Python programming.

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.