diff options
author | Danijel Andjelkovic <adanijel99@gmail.com> | 2022-03-29 12:27:29 +0200 |
---|---|---|
committer | Danijel Andjelkovic <adanijel99@gmail.com> | 2022-03-29 12:27:29 +0200 |
commit | 2bae49ecb411d13d1d9d78e764340468f7f49f44 (patch) | |
tree | 3b7907e172438775291954fb5a8b54541059acc6 /backend/microservice/PythonServer/project/socket_example | |
parent | 9f3d674413affc18377c8f2564bb660270ffe7f5 (diff) | |
parent | 98d580ca1ba501bc059ff417dd1794e0f6a68407 (diff) |
Merge branch 'trening' of http://gitlab.pmf.kg.ac.rs/igrannonica/neuronstellar into dev
# Conflicts:
# frontend/src/app/_services/models.service.ts
Diffstat (limited to 'backend/microservice/PythonServer/project/socket_example')
4 files changed, 81 insertions, 0 deletions
diff --git a/backend/microservice/PythonServer/project/socket_example/socket/client.py b/backend/microservice/PythonServer/project/socket_example/socket/client.py new file mode 100644 index 00000000..d5740e25 --- /dev/null +++ b/backend/microservice/PythonServer/project/socket_example/socket/client.py @@ -0,0 +1,10 @@ +import socket + +c = socket.socket() + +c.connect(('localhost', 9999)) + +name = input("Client name:") +c.send(bytes(name, 'utf-8')) + +print(c.recv(1024).decode())
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/socket_example/socket/server.py b/backend/microservice/PythonServer/project/socket_example/socket/server.py new file mode 100644 index 00000000..d6ff3f7c --- /dev/null +++ b/backend/microservice/PythonServer/project/socket_example/socket/server.py @@ -0,0 +1,16 @@ +import socket + +s = socket.socket() +print('Socket Created') + +s.bind(('localhost', 9999)) + +s.listen(3) +print('waiting for connections') + +while True: + c, addr = s.accept() + name = c.recv(1024).decode() + print('Connected with ', addr, name) + c.send(bytes('Welcome', 'utf-8')) + c.close()
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/socket_example/socket2/client.py b/backend/microservice/PythonServer/project/socket_example/socket2/client.py new file mode 100644 index 00000000..65e76b55 --- /dev/null +++ b/backend/microservice/PythonServer/project/socket_example/socket2/client.py @@ -0,0 +1,16 @@ +# Import socket module +import socket + +# Create a socket object +s = socket.socket() + +# Define the port on which you want to connect +port = 12345 + +# connect to the server on local computer +s.connect(('127.0.0.1', port)) + +# receive data from the server and decoding to get the string. +print (s.recv(1024).decode()) +# close the connection +s.close()
\ No newline at end of file diff --git a/backend/microservice/PythonServer/project/socket_example/socket2/server.py b/backend/microservice/PythonServer/project/socket_example/socket2/server.py new file mode 100644 index 00000000..c65dae78 --- /dev/null +++ b/backend/microservice/PythonServer/project/socket_example/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 |