-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
69 lines (52 loc) · 1.47 KB
/
server.py
File metadata and controls
69 lines (52 loc) · 1.47 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
""" Assignment 2
Computer Networks
server.py
"""
import socket
import lzma
#constants
tostore = "ser_file_sent.xz"
host = socket.gethostname()
print(host)
port = 12345
#opening the socket
s = socket.socket()
#binding it to the port, "" specifies connection allowed from different systems within same network
s.bind(("", port))
#setting the socket to listen
s.listen(5)
print("Server Online")
#Whie true specifies that server remains on irrespective
while True:
#connecting to client
c, addr = s.accept()
print('\n****************New Client connected : ', addr)
#opening a new file to store compressed data
f = lzma.LZMAFile(tostore, mode="wb")
#recieving file to compress
print("\nReceiving file to compress...")
#recieve uncompressed packets
pack = c.recv(1024)
while (pack):
print ("Receiving file to compress...")
#lzma compress and writes the data onto our file during write() command
f.write(pack)
pack = c.recv(1024)
f.close()
print ("Done Receiving file to compress")
#opening the complete compressed file to return to client
f = open(tostore,'rb')
print ('\nSending compressed file...')
#reading packets from file
pack = f.read(1024)
while (pack):
print ('Sending compressed file...')
#sending the compressed data
c.send(pack)
pack = f.read(1024)
f.close()
print ("Done Sending compressed file.")
#closing the connection to client
c.close()
#closing the socket
s.close()