-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
45 lines (35 loc) · 1 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
import sys
import socket
import threading
import argparse
ENCODING = "utf-8"
LOCAL_ENDPOINT = "127.0.0.1"
REMOTE_ENDPOINT = "74.208.200.101"
def listen():
while 1:
data = sock.recv(4096)
if data:
print("Server: " + data.decode(ENCODING))
else:
print('\nDisconnected from server')
sys.exit()
def listen_to_keypress():
while 1:
message = input("Enter text (or Enter to quit): \n")
if not message:
break
sock.sendall((message + "\n").encode(ENCODING))
parser = argparse.ArgumentParser()
parser.add_argument('--remote', action='store_true')
args = parser.parse_args()
if args.remote:
print("Attempting to connect to the remote...")
ENDPOINT = REMOTE_ENDPOINT
else:
print("Attempting to connect to the localhost...")
ENDPOINT = LOCAL_ENDPOINT
sock = socket.socket()
port = 8888
sock.connect((ENDPOINT, port))
threading.Thread(target=listen).start()
threading.Thread(target=listen_to_keypress).start()