-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
53 lines (46 loc) · 1.28 KB
/
server.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
# The Server code for Serin
# Start at a terminal and point other nodes (serin.py) to this
import socket
import lib
# Opening Socket
#--------------------------
HOST = ''
PORT = 4444
lib.printSerin()
serv = lib.createListenerHere(HOST, PORT)
serv.listen(10)
print 'Socket Listening'
#--------------------------
# The variables to store file and sender info
filename = ''
sender = ''
# Transaction management
#--------------------------
while True:
conn, adr = serv.accept()
print 'Connected to ' + adr[0] + ':' + str(adr[1])
data = conn.recv(1024)
if data != '':
# The client asks for state
if data == 'status_of_server':
print 'Client asked for state of server.'
if filename == '':
# The client is sender and is requested to give the file name.
conn.sendall('send_file')
print 'Asked client for file'
else:
# The client is receiver. File name and sender ip is sent to it.
print 'Sent client the file details'
conn.sendall(filename + ',' + sender)
filename = ''
sender = ''
else:
# The sender gives the file name.
print 'Client sent the file name : ' + data
if filename == '':
conn.sendall('ok') # Informing sender that the file is noted for sending.
filename = data
sender = adr[0]
#--------------------------
conn.close()
serv.close()