-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment02_server.py
More file actions
44 lines (40 loc) · 1.23 KB
/
assignment02_server.py
File metadata and controls
44 lines (40 loc) · 1.23 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
#!/usr/bin/python
import socket
def main():
# create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind the socket to an address and port
sock.bind(("localhost", 8888))
# start listening for new connections
sock.listen(5)
# wait for a client using accept()
# accept() returns a client socket and the address from which
# the client connected
(client, addr) = sock.accept()
print "Received a connection from", addr
# read and print whatever the client sends us
viesti=""
while True:
viesti=viesti+client.recv(1024)
# Split at first, and first only, |-character (vertical-bar) occurence
viesti_split=viesti.split("|",1)
# Check if header is received
if len(viesti_split)==2:
# If so, check if all is received
if len(viesti_split[1])==int(viesti_split[0]):
viesti=viesti_split[1]
# Break if all is received
break
print viesti
# send "hello world!" back to the client
msg="hello world! (from server :)"
# Adding header (msg length+|)
msg=str(len(msg))+"|"+msg
len_send=0
# Call client.send() until all is send
while len_send<len(msg):
len_send += client.send(msg[len_send:])
# the server proram terminates after sending the reply
sock.close()
if __name__ == "__main__":
main()