Welcome, guest | Sign In | My Account | Store | Cart

This demo chat program, at some aspect, mimic the way that Erlang makes a distributable application. Because it utilizes process, the CPU cores are used as much as possible.

Python, 80 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# Added by <ctang@redhat.com>
import sys
import os
from multiprocessing import connection


ADDR = ('', 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 response

class 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()

2 comments

Hiro 11 years, 5 months ago  # | flag

I have not managed to run it. but obviously, this code is not tested

following part will render error " def output_request(self, request): sys.stdout.write('%s says: %s\n' % request) "

Richard Galka 11 years, 5 months ago  # | flag

Hiro: You made an incorrect assumption on about the 'request' variable.

If you look through the code you can see that request is actually a list of two entries: (self.username, message) (line 63).

As an fyi for anyone else looking, on python 2.7.1 this works as expected.