-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_ds_protocol.py
46 lines (39 loc) · 1.34 KB
/
test_ds_protocol.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
# ICS 32 Winter 2022
# Final Project
#
#
# Cole Thompson / Nathan Yang
# 36762668 / 63942782
import ds_client, socket, ds_protocol, time
server = "168.235.86.101"
port = 3021
username = "hellocookiedough"
password = 'thisisapassword'
def message(client:socket, token:str):
"""This function posts a message to the server given a token and message."""
stamp = time.time() # Create a timestamp
post_msg = {"token": token, "directmessage": {"entry": "Hello World!","recipient":"ohhimark", "timestamp": stamp}}
resp = ds_client._connect(post_msg, client)
x = ds_protocol._response(resp)
return x
def request(client, message, token):
post_msg = {"token": token, "directmessage": message}
resp = ds_client._connect(post_msg, client)
x = ds_protocol._response(resp)
return x
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as c:
try:
# Connect to the server
c.settimeout(3)
c.connect((server, port))
print(f"client connected to {server} on {port}")
except:
print("Was not able to connect to server/port.")
token = ds_client.join(c, username, password)
resp = message(c, token)
assert resp == "Direct message sent"
resp = request(c, "new", token)
assert type(resp) == list
resp = request(c, "all", token)
assert type(resp) == list