Python Socket Request Handler - python

I have a problem with the actual handling of a client/ server System.
For a project I have a control unit, to which sensor data is sent.
After that, I want the Control Unit to send the data to a server, where the data is captured.
So now I have a client (HoloLens2), which connects to the server over a TCP-connection and requests data.
The Problem now, is that i don't know exactly how I should handle those different requests.
I've already coded the following lines for the server.
With this I am able to successfully open the TCP-Connection:
import socket
import threading
HEADER = 64 # Length of HEADER-message
HOST = socket.gethostbyname(socket.gethostname())
PORT = 28500 # Port to listen on (non-privileged ports are > 1023)
ADDR = (HOST, PORT)
FORMAT = 'utf-8'
DISCIONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
#Setup for listening. This function runs for each client
def handle_client(connection, cl_addr):
print(f"[NEW CONNECTION] {cl_addr} connected.")
connected = True
while connected:
#Wait on this line until we receive a message with 64 bytes
msg_length = connection.recv(HEADER).decode(FORMAT)
#Read out the information about the followed message in the HEADER-Message
msg_length = int(msg_length)
#Receive the actual message
msg = connection.recv(msg_length).decode(FORMAT)
if(msg == DISCIONNECT_MESSAGE):
connected = False
print(f"[{cl_addr}] {msg}")
connection.close()
#start socket server
def start():
server.listen()
print(f"[LISTENING] Server is listening on {HOST}")
while True:
#Wait on this line until new connection established
conn, addr = server.accept()
#when connection successfull, start new thread
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1} ")
print("[STARTING] server is starting . . .")
start()
If I now want to send different data for different requests, for example when the message is "!SENSOR1",
I want to send all the data captured of the sensor.
Do I have to implement the whole thing simply with if-else query like this:
if(msg == "!SENSOR1"):
connection.send("This is the data from sensor 1: ")
Or is there a much more efficient way to handle this problem?

Related

Python console chat with socket

I'm writing a simple console chat with server and client. When receiving a message from the first client server should send it to the second client and vice versa. But when first client sends a message to the server it returns back and doesn't reach the second client. Maybe there is a problem in receiving() function.
Here is my client.py:
import socket
from _thread import *
def recieving(clientSocket):
while True:
encodedMsg = clientSocket.recv(1024)
decodedMsg = encodedMsg.decode('utf-8')
print(decodedMsg)
def chat(clientSocket, name):
msg = input()
encoded_msg = f'[{name}] {msg}'.encode('utf-8')
clientSocket.send(encoded_msg)
def main():
serverAddress = (socket.gethostname(), 4444)
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(serverAddress)
name = input('Enter your name: ')
start_new_thread(recieving, (clientSocket,))
while True:
chat(clientSocket, name)
if __name__ == "__main__":
main()
And server.py:
import time
import socket
from _thread import *
def listen(clientSocket, addr):
while True:
encodedMsg = clientSocket.recv(1024)
decodedMsg = encodedMsg.decode('utf-8')
currTime = time.strftime("%Y-%m-%d-%H.%M.%S", time.localtime())
for client in clients:
if addr != client:
clientSocket.sendto(encodedMsg, client)
print(f'[{currTime}] {decodedMsg}')
def main():
serverAddress = (socket.gethostname(), 4444)
global clients
clients = []
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind(serverAddress)
serverSocket.listen(2)
while True:
clientSocket, addr = serverSocket.accept()
if addr not in clients:
clients.append(addr)
print(f'{addr} joined chat')
start_new_thread(listen, (clientSocket, addr))
if __name__ == '__main__':
main()
sendto doesn't work as expected if its socket is connected. It just sends to the connected socket, not the specified address.
Therefore, listen needs to be able to access the open socket of each client in order to write to it.
Currently clients is a list of addresses, but you could change it to a dict of address to socket mappings:
def main():
global clients
clients = {}
Then when you get a new client connection, save address and socket:
clientSocket, addr = serverSocket.accept()
if addr not in clients:
clients[addr] = clientSocket
print(f'{addr} joined chat')
start_new_thread(listen, (clientSocket, addr))
Finally, in listen, write to each other client's socket, not the connected clientSocket for that listen thread:
for client in clients:
if addr != client:
print(f"sending message from {addr} to {client}")
clients[client].send(encodedMsg)
There's a number of other problems with your code.
Sockets are not thread safe. So there is a race condition if 2 clients happen to write the same thing at the same time; the writes could be interpolated and the messages munged up.
If a client disconnects, the server doesn't handle the disconnection well. If the server disconnects, the clients go into an infinite loop as well.

Socket programming - Communication between server and client

I'm trying to learn about sockets and how to create a server and a client in python.
While reading this great article from Real Python I had difficulties understanding why the server receives two strings, when I only send one.
server.py
import socket
HOST = "127.0.0.1"
PORT = 65432
server = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM
)
with server:
server.bind((HOST, PORT))
server.listen()
print("Waiting for connections...")
conn, addr = server.accept()
print("Accepted!")
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
print(f"Message received: {data}")
if not data:
print(f"Breaking while loop and closing connection")
break
conn.sendall(data)
client.py
import socket
HOST = "127.0.0.1"
PORT = 65432
client = socket.socket(
family=socket.AF_INET,
type=socket.SOCK_STREAM
)
with client as c:
c.connect((HOST, PORT))
# Get input from client
message = input("Enter your message: ")
c.sendall(str.encode(message))
data = c.recv(1024)
print(f"Received {data}")
Output from server.py after running the server and client:
Waiting for connections...
Accepted!
Connected by ('127.0.0.1', 64476)
Message received: b'message'
Message received: b''
Breaking while loop and close connection
Why does the server receive two messages (b'message' and b'')
The recv() can only empty string when the other end is gone. You are unable to send zero length data over socket (try it :). So the fact you are seeing this is simply because you are not checking for that.
PS: your client's last print() is not correctly indented.

Cannot send data to two clients at the same time when using sockets in python

I'm currently trying to make a socket server and client program in which the server is able to send simple strings of text to the clients. As of now even when two clients are recognized and established, the message only gets sent to whichever connected first. I've been moving around lots of my code and trying to solve the problem but nothing seems to work. My code for the server is immediately below followed by the client code. Everything works when I only connect to one client; however, as soon as a second connection is made and recognized it continues to only send to the first client while the second client stays in a waiting process. Any help is appreciated and thank you!
server code
import socket
import threading
import time
HEADER = 2048
PORT = 4444
SERVER = server ip (redacted)
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "dc"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f"[NEW CONNECTION] {addr} connected.")
while True:
comman = input("message: ")
conn.send(comman.encode(FORMAT))
def start():
server.listen()
print(f"[LISTENING] Server is listening on {SERVER}")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
print("server is starting...")
start()
client code
import socket
from sys import stdin
import time
import subprocess
import os
HEADER = 2048
PORT = 4444
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "dc"
SERVER = server ip (redacted)
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def recieve():
while True:
print(client.recv(2048).decode(FORMAT))
recieve()

How to connect to server, listen for arbitrary time and send back data

I'm trying to:
Connect to a server/port
Listen for x seconds
Receive user input
Send user input to server
Go back to step 2
So far, I've written the following code, but it's not working properly receiving input after the first send. Any help would be greatly appreciated.
import socket
import select
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('domain.com', 1234))
client_socket.setblocking(0)
timeout = 5
while True:
while True:
ready = select.select([client_socket], [], [], timeout)
if ready[0]:
data = client_socket.recv(4096)
print data
else:
break
data = raw_input("Enter input:")
client_socket.send(data)
You need to have separate server side code and client side code. This article has been referred.
Server binds to a port and listens for clients
server.py
import select
import socket
# Create a TCP/IP socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)
# Bind the socket to the port
server_address = ('localhost', 1234)
server.bind(server_address)
# Listen for incoming connections
server.listen(5)
# Sockets from which we expect to read
inputs = [ server ]
# Sockets to which we expect to write
outputs = [ ]
while inputs:
readable, writable, exceptional = select.select(inputs, outputs, inputs)
# Handle inputs
for s in readable:
if s is server:
# A "readable" server socket is ready to accept a connection
connection, client_address = s.accept()
connection.setblocking(0)
inputs.append(connection)
else:
data = s.recv(1024)
if data:
print "Receiving data from client"
print data
else:
inputs.remove(s)
s.close()
Client first establishes a connection with the server and then keeps on sending user input to the server.
client.py
import socket
server_address = ('domain.com', 1234)
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(server_address)
while True:
data = raw_input("Enter input:")
sock.send(data)
Open terminal.
Run server in background:
python server.py &
Run client after that:
python client.py

Sending string via socket (python)

I have two scripts, Server.py and Client.py.
I have two objectives in mind:
To be able to send data again and again to server from client.
To be able to send data from Server to client.
here is my Server.py :
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
serversocket.listen(5)
print ('server started and listening')
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
and here is my client :
#! /usr/bin/python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host ="192.168.1.3"
port =8000
s.connect((host,port))
def ts(str):
s.send('e'.encode())
data = ''
data = s.recv(1024).decode()
print (data)
while 2:
r = input('enter')
ts(s)
s.close ()
The function works for the first time ('e' goes to the server and I get return message back), but how do I make it happen over and over again (something like a chat application) ?
The problem starts after the first time. The messages don't go after the first time.
what am I doing wrong?
I am new with python, so please be a little elaborate, and if you can, please give the source code of the whole thing.
import socket
from threading import *
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
print('Client sent:', self.sock.recv(1024).decode())
self.sock.send(b'Oi you sent something to me')
serversocket.listen(5)
print ('server started and listening')
while 1:
clientsocket, address = serversocket.accept()
client(clientsocket, address)
This is a very VERY simple design for how you could solve it.
First of all, you need to either accept the client (server side) before going into your while 1 loop because in every loop you accept a new client, or you do as i describe, you toss the client into a separate thread which you handle on his own from now on.
client.py
import socket
s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
str = raw_input("S: ")
s.send(str.encode());
if(str == "Bye" or str == "bye"):
break
print "N:",s.recv(1024).decode()
s.close()
server.py
import socket
s = socket.socket()
port = 12345
s.bind(('', port))
s.listen(5)
c, addr = s.accept()
print "Socket Up and running with a connection from",addr
while True:
rcvdData = c.recv(1024).decode()
print "S:",rcvdData
sendData = raw_input("N: ")
c.send(sendData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
c.close()
This should be the code for a small prototype for the chatting app you wanted.
Run both of them in separate terminals but then just check for the ports.
This piece of code is incorrect.
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
The call on accept() on the serversocket blocks until there's a client connection. When you first connect to the server from the client, it accepts the connection and receives data. However, when it enters the loop again, it is waiting for another connection and thus blocks as there are no other clients that are trying to connect.
That's the reason the recv works correct only the first time. What you should do is find out how you can handle the communication with a client that has been accepted - maybe by creating a new Thread to handle communication with that client and continue accepting new clients in the loop, handling them in the same way.
Tip: If you want to work on creating your own chat application, you should look at a networking engine like Twisted. It will help you understand the whole concept better too.

Categories