-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChat_old.py
68 lines (54 loc) · 1.72 KB
/
Chat_old.py
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
import argparse
import random
import time
import zmq
# def poll_socket(socket, timetick = 100):
# raise Exception("do not use")
# poller = zmq.Poller()
# poller.register(socket, zmq.POLLIN)
# try:
# obj = dict(poller.poll(timetick))
# if socket in obj and obj[socket] == zmq.POLLIN:
# yield socket.recv()
# except KeyboardInterrupt:
# pass
def run_server(context, ip, port):
socket = context.socket(zmq.REP)
# socket.RCVTIMEO = 1000
# del ip
# ip = "*"
# ip = "0.0.0.0"
socket.bind("tcp://{0}:{1}".format(ip, port))
while True:
print("waiting to receive request ...")
received = socket.recv()
print("received:", received)
reply = str(random.random())
socket.send_string(reply)
def run_client(context, ip, port):
socket = context.socket(zmq.REQ)
socket.RCVTIMEO = 100000
socket.connect("tcp://{0}:{1}".format(ip, port))
while True:
request = str(random.random())
socket.send_string(request)
print("waiting to receive reply ...")
try:
received = socket.recv()
print("received:", received)
except:
raise RuntimeError("server failed to reply")
time.sleep(1)
tessa_ip = "73.177.69.155" # gotten by googling "what is my ip" which doesn't actually work
localhost_ip = "127.0.0.1"
wesley_ip = "192.168.1.7" # from `ipconfig /all`
ip_to_use = wesley_ip
context = zmq.Context()
parser = argparse.ArgumentParser()
parser.add_argument("--server", action="store_true")
args = parser.parse_args()
server_port = "5000"
if args.server:
run_server(context, ip_to_use, server_port)
else:
run_client(context, ip_to_use, server_port)