-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
107 lines (88 loc) · 2.5 KB
/
client.py
File metadata and controls
107 lines (88 loc) · 2.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import socket
import time
import tkinter
# from tkinter import *
from invite import return_invite_key
import threading
from config import *
try:
username = get_uname()
username = str(username)
print(username)
except:
# open the username file and read it
file = open('username.txt', 'r')
# read username
username = file.read()
# close the file
file.close()
# username = user
# open the roling file and read it
file = open('role.txt', 'r')
# get invite and just return back host and port
# not totally encrypted for now
def decrypt_invite(invite:str):
host_name_n_port = invite.split('&')
return host_name_n_port[0], host_name_n_port[1]
try:
inside = get_conn_key()
except:
inside = file.read()
file.close()
print(inside)
# if host get itself's host and port created by ngrok
if inside == "host":
host, port = decrypt_invite(return_invite_key())
else: # get the invite
# split the role and the key
all = inside.split(':')
print(all)
# split host and port in the key
all = all[1].split('&')
# assign host and port
host = all[0]
port = all[1]
# create socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# start streaming
try:
s.connect((host, int(port)))
except:
print("There is a problem with invitation key! ")
# get data from server and used by to gui
def recieve(s: socket.socket, listbox: tkinter.Listbox):
while True:
try:
# how big of data is determined, buffer size
msg = s.recv(1024)
print(msg.decode("utf-8"))
time.sleep(1)
listbox.insert(tkinter.END, msg)
if msg == 'q':
break
except:
time.sleep(1)
s.close()
# get data from server and use it in movie messaging part
def recieve_movie(s:socket.socket, listbox: tkinter.Listbox):
while True:
try:
# how big of data is determined, buffer size
msg = s.recv(1024)
print(msg.decode("utf-8"))
if msg.decode("utf-8")[0] == '!':
pass
else:
listbox.insert(tkinter.END, msg)
time.sleep(1)
if msg == 'q':
break
except:
time.sleep(1)
s.close()
# send data to server
def send(s:socket.socket, username:str, msg:str):
all = username + "&" + msg
s.send(all.encode())
# rec = threading.Thread(target=recieve, args=(s,))
sen = threading.Thread(target=send, args=(s,username))