forked from fifthist/sp24-proj0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
49 lines (41 loc) · 1.25 KB
/
client.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
"""
A client for connecting to a RookieDB server.
See src/main/java/edu/berkeley/cs186/database/cli/Server.java
for details
"""
import socket
import threading
import sys
import argparse
DEFAULT_PORT = 18600
DEFAULT_HOST = "localhost"
def receive(s):
while True:
data = s.recv(1024)
if len(data) == 0:
break
print(data.decode("utf-8"), end="")
sys.stdout.flush()
def main(host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
receive_thread = threading.Thread(target=receive, args=(s,))
receive_thread.start()
try:
while True:
inp = input()
if inp.replace(";", "").strip().lower() == "exit":
break
s.send((inp + "\n").encode("utf-8"))
except (EOFError, KeyboardInterrupt):
# User sent ctrl+D or ctrl+C
print("exit")
finally:
s.shutdown(socket.SHUT_RDWR)
s.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Connect to a RookieDB server.")
parser.add_argument("--host", default=DEFAULT_HOST)
parser.add_argument("--port", "-p", default=DEFAULT_PORT, type=int)
args = parser.parse_args()
main(args.host, args.port)