-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-side.py
176 lines (155 loc) · 4.75 KB
/
server-side.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import socket
import sys
import time
import os
# The animation of printing
def text_out(text: str) -> None:
for character in text:
sys.stdout.write(character)
sys.stdout.flush()
time.sleep(0.1)
class file:
def separate(data):
texts=data.split("<_SEPARATOR_>")
datas=texts[0]
ext=texts[1]
return datas,ext
def compress(contents,filename):
message=contents+"<_SEPARATOR_>"+filename
return message
def findExtension(file):
if "/" in file:
splitted=file.split("/")
filename=splitted[-1:]
return filename[0]
else:
filename=file
return filename
def openFile(file_dir,ext):
extAll=ext.split(".")
extension=extAll[1]
all="jpg jpeg gif JPEG tiff mp4 mp3 dox ppx avi pdf wav ogg zip bin csv exe ico svg bat dat sql tar gz "
if extension in all:
return ''
else:
f=open(file_dir,"rt")
contents=f.read()
return contents
print("\n [ ] SERVER CONFIGURATIONS - ")
host=input(" [#] - Enter Your server Address:: ")
if host=="":
host="localhost"
text_out(" [ Default ] :: localhost")
print("")
else:
pass
while True:
portStr=input(" [#] - Port Number (1000-9999):: ")
try:
port = int(portStr)
except:
port = 12345678
if (type(port) is int) and (port>=1000 and port<=9999):
break
else:
text_out(" [ Error ] :: invalid port!!\n")
print("\n")
bufferSize=1048576
server=socket.socket()
server.bind((host,port))
text_out(" [ Waiting... ] :: connections from clients...")
server.listen(2)
conn, addrs=server.accept()
os.system("clear")
text_out(f" [ connected ] :: server connected to {addrs[0]} at {addrs[1]}")
print("")
print("\n\n")
text_out(" [ chat ] :: A MESSAGE FROM THE CLIENT !!!")
print("")
print("\n")
while True:
try:
# RECEIVING
data=conn.recv(bufferSize).decode()
text_out("[ client ]:: ")
received=str(data)
if(received=="STOPPED"):
text_out("\n [ problem ] :: client has some problems!! ")
print("")
break
if type(received) is str:
compare=int(received.find("<_SEPARATOR_>"))
if(compare>=0):
contents,filename=file.separate(received)
text_out(f"\n [ received ] :: - {filename} ")
print("")
file_path=input(" [ directory ] :: Where should the file be saved? \n [ default ] :: Present Directory:: ")
if(file_path==""):
try:
f=open(filename,"w")
f.write(f'''{contents}''')
f.close()
except:
conn.send("STOPPED".encode())
text_out(" [ error ] :: Receiving the file from client")
print("")
break
text_out(" [ received ] :: current directory!!")
print("")
else:
full=filepath+filename
try:
f=open(full,"w")
f.write(f'''{contents}''')
f.close()
except:
conn.send("STOPPED".encode())
text_out(f" [ error ] :: receiving file from client :: at {full}!!")
print("")
break
text_out(f" {filename} successfully saved!! ")
print("")
else:
text_out(received)
print("")
else:
text_out(" [ 403 ]: A non-text file received!!")
print("")
break
# SENDING
message=input("[ You ]:: ")
message=f'''{message}'''
if(message=="quit" or message=="q"):
conn.send("STOPPED".encode())
text_out(" [ 200 ] :: server shutteddown conditionally!!")
print("")
break
elif(message=="file"):
# when wanting to send files
try:
file_dir=input(" [File-Name]:: ")
extension=file.findExtension(file_dir)
contents=file.openFile(file_dir,extension)
if(contents==''):
print('')
text_out(" [ 403 ] :: The type of the file you want to send is forbidden!!")
print('')
conn.send("STOPPED".encode())
break
message=file.compress(contents,extension)
except:
#using this in debug mode
conn.send("STOPPED".encode())
text_out(" [ doesn't Exist ] :: No such File or Directory!!")
print("")
break
conn.send(message.encode())
except KeyboardInterrupt:
conn.send("STOPPED".encode())
text_out(" [ 200 ] :: Turning off server...")
print("")
break
conn.close()
text_out(" [ 200 ] :: The server turned off successfully!!")
print("")
server.close()