-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
42 lines (35 loc) · 1.11 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
# first of all import the socket library
import socket
# next create a socket object
s = socket.socket()
print("Socket successfully created")
# reserve a port on your computer in our
# case it is 12345 but it can be anything
port = 12345
# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))
print ("socket binded to %s" %(port))
# put the socket into listening mode
s.listen(5)
print( "socket is listening")
# a forever loop until we interrupt it or
# an error occurs
while True:
# Establish connection with client.
c, addr = s.accept()
print ('Got connection from', addr)
a = c.recv(1024)
a = a.decode('utf-8')
a = a.split(':')
print(a)
if(a[0]=='divide'):
retval = int(a[1])/int(a[2])
# send a thank you message to the client.
retval = str(retval)
retval = retval.encode('utf-8')
c.send(retval)
c.close()