From 39228ed08db5aa56e6a1c02ddbfc6691e1d0eef0 Mon Sep 17 00:00:00 2001 From: Nevena Bojovic Date: Tue, 15 Mar 2022 23:10:12 +0100 Subject: Druga verzija - Python socket. --- .../PythonServer/project/api/socket2/server.py | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 backend/microservice/PythonServer/project/api/socket2/server.py (limited to 'backend/microservice/PythonServer/project/api/socket2/server.py') diff --git a/backend/microservice/PythonServer/project/api/socket2/server.py b/backend/microservice/PythonServer/project/api/socket2/server.py new file mode 100644 index 00000000..c65dae78 --- /dev/null +++ b/backend/microservice/PythonServer/project/api/socket2/server.py @@ -0,0 +1,39 @@ +# 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 ) + + # send a thank you message to the client. encoding to send byte type. + c.send('Thank you for connecting'.encode()) + + # Close the connection with the client + c.close() + + # Breaking once connection closed + break \ No newline at end of file -- cgit v1.2.3