-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatclient.py
More file actions
82 lines (67 loc) · 2.52 KB
/
chatclient.py
File metadata and controls
82 lines (67 loc) · 2.52 KB
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
81
82
# Simple chat client to communicate with chat script server
# Not very efficient, since it uses a thread per socket model,
# If servicing a large number of clients, twisted may be a better fit
from optparse import OptionParser
import socket
import sys
def sendAndReceiveChatScript(msgToSend, server='127.0.0.1', port=1024, timeout=10):
try:
# Connect, send, receive and close socket. Connections are not persistent
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout) # timeout in secs
s.connect((server, port))
s.sendall(msgToSend)
msg = ''
while True:
chunk = s.recv(1024)
if chunk == b'':
break
msg = msg + chunk.decode("utf-8")
s.close()
return msg
except:
return None
if __name__ == '__main__':
server = "127.0.0.1"
port = 1024
botname = ""
# Setup the command line arguments.
optp = OptionParser()
# user name to login to chat script as
optp.add_option("-u", dest="user", help="user id, required")
# botname
optp.add_option("-b", dest="botname", help="which bot to talk to, if not specified, will use default bot")
# server
optp.add_option("-s", dest="server", help="chat server host name (default is " + str(server) + ")")
# port
optp.add_option("-p", dest="port", help="chat server listen port (default is " + str(port) + ")")
opts, args = optp.parse_args()
if opts.user is None:
optp.print_help()
sys.exit(1)
user = opts.user
if opts.botname is not None:
botname = opts.botname
if opts.server is not None:
server = opts.server
if opts.port is not None:
port = int(opts.port)
print("Hi " + user + ", enter ':quit' to end this session")
while True:
s = input("[" + user + "]" + ">: ").lower().strip()
if s == ':quit':
break
# Ensure empty strings are padded with atleast one space before sending to the
# server, as per the required protocol
if s == "":
s = " "
# Send this to the server and print the response
# Put in null terminations as required
msg = u'%s\u0000%s\u0000%s\u0000' % (user, botname, s)
msg = str.encode(msg)
resp = sendAndReceiveChatScript(msg, server=server, port=port)
if resp is None:
print("Error communicating with Chat Server")
break # Stop on any error
else:
print("[Bot]: " + resp)