-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReceiver.py
More file actions
116 lines (95 loc) · 2.65 KB
/
Copy pathReceiver.py
File metadata and controls
116 lines (95 loc) · 2.65 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
108
109
110
111
112
113
114
115
import socket
from cryptography.fernet import Fernet
A = 9998
def create_socket():
try:
global host
global port
global s
host = ""
port = A
s = socket.socket(socket.AF_INET , socket.SOCK_STREAM)
except socket.error as e:
print("socket creation error" + str(e))
def bind_socket():
try:
global host
global port
global s
s.bind((host,port))
print("Binding to the post : "+str(port))
s.listen(5)
except socket.error as e:
print("Socket Binding error "+"\n"+'Retrying ...')
bind_socket()
def send_command(conn):
while True:
#using infinte we can send more than one command
try :
client_response = str(conn.recv(256456),"utf-8")
print(client_response)
if client_response == "Disconnect":
print("Disconnecting from Client")
break
elif client_response[0:5] == "Image":
index = client_response.index("^")
size = int(client_response[5:index])
file_name = client_response[index + 1:]
print(size)
print(file_name)
client_response = conn.recv(256456)
print(client_response)
f = Fernet(key)
a = f.decrypt(client_response)
print(a)
with open(file_name, 'wb') as fd:
fd.write(a)
elif client_response[0:5] == "Audio":
index = client_response.index("^")
size = int(client_response[5:index])
file_name = client_response[index + 1:]
print(size)
print(file_name,"*/*/*")
client_response = conn.recv(2097152)
print(len(client_response))
f = Fernet(key)
#a = f.decrypt(client_response)
a = client_response
print(a)
with open(file_name, 'wb') as fd:
fd.write(a)
print("-----------------------------")
else:
client_response = str.encode(client_response)
f = Fernet(key)
a = f.decrypt(client_response)
client_response = str(a, 'utf-8')
if client_response[0:4] == "File":
client_response = client_response[4:]
index = client_response.index('^')
file_name = client_response[0:index]
client_response = client_response[index+1:]
print(file_name,"*")
with open(file_name,'w') as f:
f.write(client_response)
#print(client_response)
else:
with open("stores.txt",'a') as fa:
fa.write(client_response+"\n")
except:
continue
def socket_accept():
global key
conn , address = s.accept()
print("Connection has been established! \n"+"IP : " + address[0] + " | Port Number : " + str(address[1]))
key = str(conn.recv(1024),"utf-8")
print(key)
send_command(conn)
#print(key)
conn.close()
#main function
def main():
create_socket()
bind_socket()
socket_accept()
main()