source: fact/trigger/TestClient_prov.py@ 13151

Last change on this file since 13151 was 52, checked in by rissim, 15 years ago
trigger software (VME) v1
  • Property svn:executable set to *
File size: 3.5 KB
Line 
1#!/usr/bin/python
2#############################################################################
3# The chat client
4#############################################################################
5
6"""
7Simple chat client for the chat server. Defines
8a simple protocol to be used with chatserver.
9
10"""
11
12import socket
13import sys
14import select
15import readline
16import threading
17import time
18from communication import send, receive
19
20BUFSIZ = 1024
21
22
23class MyUserInput(threading.Thread):
24 UserInput=''
25 ReceivedData=''
26 def run(self):
27 while (self.UserInput!="exit" and self.UserInput!="EXIT" and self.ReceivedData!="exit" and self.ReceivedData!="EXIT"):
28 dummy=raw_input("CLIENT >>>")
29 if(self.UserInput!="exit" and self.UserInput!="EXIT" and self.ReceivedData!="exit" and self.ReceivedData!="EXIT"):
30 self.UserInput=dummy
31 else:
32 break
33 time.sleep(0.5)
34
35class ChatClient(object):
36 """ A simple command line chat client using select """
37
38 def __init__(self, name, host='127.0.0.1', port=3490):
39 self.myUserInput=MyUserInput()
40 self.myUserInput.daemon=True
41 self.myUserInput.start()
42 self.name = name
43 # Quit flag
44 self.flag = False
45 self.port = int(port)
46 self.host = host
47 # Initial prompt
48 self.prompt='[' + '@'.join((name, socket.gethostname().split('.')[0])) + ']> '
49 # Connect to server at port
50 try:
51 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52 self.sock.connect((host, self.port))
53 print 'Connected to chat server@%d' % self.port
54 # Send my name...
55 send(self.sock,'NAME: ' + self.name)
56 data = receive(self.sock)
57 # Contains client address, set it
58 addr = data.split('CLIENT: ')[1]
59 self.prompt = '[' + '@'.join((self.name, addr)) + ']> '
60 except socket.error, e:
61 print 'Could not connect to chat server @%d' % self.port
62 sys.exit(1)
63
64 def cmdloop(self):
65 oldUserInput=''
66 while not self.flag:
67 try:
68 #sys.stdout.write(self.prompt)
69 #sys.stdout.flush()
70
71 # Wait for input from stdin & socket
72 inputready, outputready,exceptrdy = select.select([self.sock], [],[])
73
74 for i in inputready:
75 #if i == 0:
76 # data = sys.stdin.readline().strip()
77 # if data: send(self.sock, data)
78 if i == self.sock:
79 data = receive(self.sock)
80 if not data:
81 print 'Shutting down.'
82 self.flag = True
83 break
84 else:
85 sys.stdout.write(data + '\n')
86 sys.stdout.flush()
87 if(oldUserInput!=self.myUserInput.UserInput):
88 oldUserInput=self.myUserInput.UserInput
89 send(self.sock,self.myUserInput.UserInput)
90
91 except KeyboardInterrupt:
92 print 'Interrupted.'
93 self.sock.close()
94 break
95
96
97if __name__ == "__main__":
98 import sys
99
100 if len(sys.argv)<3:
101 sys.exit('Usage: %s chatid host portno' % sys.argv[0])
102
103 client = ChatClient(sys.argv[1],sys.argv[2], int(sys.argv[3]))
104 client.cmdloop()
105
Note: See TracBrowser for help on using the repository browser.