When are socket buffers deemed ready for reading/writing? - python

I have two pieces of code, a server and a client for a chat application using python 'socket' and 'selectors' modules. The 'service_connection' function should only do something when a socket is ready for reading. However as far as i'm aware, all the client does is send a message containing the client's username. This is dealt with in 'accept_connection' function. However I then receive an error resulting from the 'service_connection' function. Why is my program getting to this point?
Client:
import socket
import selectors
HEADER_LENGTH = 10
HOST = '127.0.0.1'
PORT = 54321
username = input('Enter username: ')
# Set up client socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
sock.setblocking(False)
# Send username to server with a header
username_enc = username.encode("utf-8")
user_header = f'{len(username_enc):<{HEADER_LENGTH}}'.encode("utf-8")
sock.send(user_header + username_enc)
Server:
import socket
import selectors
HEADER_LENGTH = 10
HOST = '127.0.0.1'
PORT = 54321
clients = [] # List for storing client information
def accept_connection(sock):
"""Function to accept a new connection"""
conn, addr = sock.accept()
print(f'Connected to by {addr}')
conn.setblocking(False)
# Receive first message. This will contain username
user_header = conn.recv(HEADER_LENGTH)
username_length = int(user_header.decode("utf-8").strip())
username = conn.recv(username_length).decode("utf-8")
# Register socket with sel
events = selectors.EVENT_READ | selectors.EVENT_WRITE
data = {"addr": addr, "username": username, "socket": conn}
sel.register(conn, events, data=data)
clients.append(data)
def service_connection(key, mask):
"""Function to service an existing connection"""
sock = key.fileobj
data = key.data
# Check for read events
if mask & selectors.EVENT_READ:
# Receive message from socket
msg_header = sock.recv(HEADER_LENGTH)
if msg_header is None:
print(f'Closing connection to {data.addr}')
sel.unregister(sock)
sock.close()
clients = [client for client in clients if client.addr!=data.addr]
return None
msg_length = int(msg_header.decode("utf-8").strip())
msg = sock.recv(msg_length).decode("utf-8")
# Create username header
username_enc = data.username.encode("utf-8")
user_header = f'{len(username_enc):<{HEADER_LENGTH}}'.encode("utf-8")
# Distribute message to all other connected clients
for client in clients:
if client.addr == data.addr:
continue
client.socket.send(user_header + msg_header + msg.encode())
# Set up listening socket
lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
lsock.bind((HOST, PORT))
lsock.listen()
lsock.setblocking(False)
# Set up selectors object
sel = selectors.DefaultSelector()
sel.register(lsock, selectors.EVENT_READ, data=None)
# Loop over blocking calls to select
while True:
events = sel.select()
for key, mask in events:
if key.data is None:
# Listening socket is ready. Accept new connection
accept_connection(key.fileobj)
else:
# Existing socket is ready. Service it
service_connection(key, mask)
Both server and client run as expected. However once I provide a username in the client script, I receive the following output server-side:
Connected to by ('127.0.0.1', 62636)
Traceback (most recent call last):
File "server.py", line 78, in <module>
service_connection(key, mask)
File "server.py", line 44, in service_connection
msg_length = int(msg_header.decode("utf-8").strip())
ValueError: invalid literal for int() with base 10: ''

This is because when the client script is finished it sends 0 bytes to the server and closes the socket connection. The server-side socket therefore has 0 bytes ready for reading. For service_connection() to work properly the line if msg_header is None: needs to be changed to if not msg_header:

Related

Python sockets and commands

I'm trying to send console commands from one machine to another using Python sockets. I want the server to send back the results of the command to the client. If the client types "ls" I want the server to send back the results of running that command. Instead of the expected result, the server just says "action completed: ls". How can I fix this so the server will run the expect commands and return the result?
Server:
import socket
from subprocess import call
def main():
host = '127.0.0.1'
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
c, addr = s.accept()
print('Connection established: ' + str(addr))
while True:
try:
data = c.recv(1024).decode('utf-8')
print('sending data: ' + data)
c.send(data.encode('utf-8'))
if data == 'q':
break
except NameError:
error = 'Command does not exist'
c.send(error.encode('utf-8'))
continue
except SyntaxError:
error = 'Command does not exist'
c.send(error.encode('utf-8'))
continue
c.close()
Client:
import socket
from subprocess import call
def main():
host = '127.0.0.1'
port = 5000
s = socket.socket()
s.connect((host, port))
message = str(input('> '))
while message != 'q':
try:
s.send(message.encode('utf-8'))
data = s.recv(1024).decode('utf-8')
print('Action completed: %s' % data)
message = str(input('> '))
except NameError:
print("Command not recognized.")
continue
except SyntaxError:
print("Command not recognized")
continue
I recently built a socket connection in order to communicate with an android device.
I decided to use UDP instead of TCP (which is what you did). For UDP as well as TCP you need a sender and a receiver on both sides of the communication.
The port number that is received in the "addr" variable changes with every connection, so you cannot use it.
What I did, I assigned two different ports one for sending from A to B and the other port to send from B to A.
Here is my server code:
import socket # socket connection
import threading # Multithreading
import time # Timeing
# ----------------------------------------------
# Variables
# ----------------------------------------------
UDPListen2Port = 12345
UDPSend2Port = 123456
Listen2IP = '' # input your local IP here
# ----------------------------------------------
# Threading class
# ----------------------------------------------
class signalProcessingThread(threading.Thread):
def __init__(self, iP, cmdIn):
threading.Thread.__init__(self)
self.iP = iP
self.cmdIn = cmdIn
def run(self):
print("Recv--", self.iP ,"--", self.cmdIn) # Display Thread Info
cmdOut = self.EvalMessage() # Actual signal processing
byteOut = bytes(cmdOut.encode("utf-8")) # Convert Server reply to bytes
sock.sendto(byteOut,(self.iP,UDPSend2Port)) # Send Server Reply to Socket
# ----------------------------------------------
# Initialize Socket
# ----------------------------------------------
sock = socket(AF_INET, SOCK_DGRAM) # -- UDP -- connection
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) # in case the port was not properly closed before
sock.bind((Listen2IP,UDPListen2Port)) # bind to the port
# ----------------------------------------------
# Listen to Socket
# ----------------------------------------------
while True:
try: # wait for a connection
data,addr = sock.recvfrom(66507) # number of bytes in the message
msg = data.decode('utf-8')
newThread = signalProcessingThread(addr[0],msg)
newThread.start()
except KeyboardInterrupt:
print('Connection failed')
sock.close()
sock.close()
The client code is quite similar, with the difference that it doesn't necessarily need to run in a thread. Hope I could help.

Show all the clients connected to a socket server and send them data

I have this simple code:
import socket
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen()
while True:
client_socket, addr = socket.accept()
send = input("Send: ") # but I need a way to send it to all the clients connected
if send == "devices":
# here I'd have a list of all devices connected
client_socket.send(send.encode())
data = client_socket.recv(4096)
print (data)
As I wrote in the comments, I need a way to manage them all in one. How can I do? Maybe with _thread library?
You could mainitain a list of clients that can be passed to an external function that performs an action on all clients.
import socket
host = ''
port = 1000
max_connections = 5
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
clients = [] # Maintain a list of clients
try:
while True:
client_socket, addr = socket.accept()
clients.append(client_socket) #Add client to list on connection
i_manage_clients(clients) #Call external function whenever necessary
except KeyboardInterrupt:
socket.close()
def i_manage_clients(clients): #Function to manage clients
for client in clients:
client.send('Message to pass')
The above example demonstrates how send data to all clients at once. You could use the
import socket
from thread import *
host = ''
port = 1000
max_connections = 5
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.bind((host, port))
socket.listen(max_connections)
try:
while True:
client_socket, addr = socket.accept()
start_new_thread(i_manage_client, (client_socket,addr))
except KeyboardInterrupt:
socket.close()
def i_manage_client(client_socket, addr): #Function to manage clients
client_socket.send('Message to pass')
data = client_socket.recv(4096)
print(client_socket)
print(addr)
print(data)

Error: Transport endpoint is not connected (Python Sockets)

I'm trying to create a simple chat application using sockets in Python (with threads). Application is simple client has to threads one to send data and another to receive. Server has to two threads one to accept client connection and another to broadcast the message. But on running the below code, I'm getting error message
Transport endpoint is not connected
Can anybody tell me why I'm getting this error
Client
import socket, threading
def send():
msg = raw_input('Me > ')
cli_sock.send(msg)
def receive():
data = cli_sock.recv(4096)
print('> '+ str(data))
if __name__ == "__main__":
# socket
cli_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect
HOST = 'localhost'
PORT = 5028
cli_sock.connect((HOST, PORT))
print('Connected to remote host...')
thread_send = threading.Thread(target = send)
thread_send.start()
thread_receive = threading.Thread(target = receive)
thread_receive.start()
Server
import socket, threading
def accept_client():
while True:
#accept
cli_sock, cli_add = ser_sock.accept()
CONNECTION_LIST.append(cli_sock)
print('Client (%s, %s) connected' % cli_add)
def broadcast_data():
while True:
data = ser_sock.recv(4096)
for csock in CONNECTION_LIST:
try:
csock.send(data)
except Exception as x:
print(x.message)
cli_sock.close()
CONNECTION_LIST.remove(cli_sock)
if __name__ == "__main__":
CONNECTION_LIST = []
# socket
ser_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind
HOST = 'localhost'
PORT = 5028
ser_sock.bind((HOST, PORT))
# listen
ser_sock.listen(1)
print('Chat server started on port : ' + str(PORT))
thread_ac = threading.Thread(target = accept_client)
thread_ac.start()
thread_bd = threading.Thread(target = broadcast_data)
thread_bd.start()
You're using server sockets incorrectly. You cannot recv on server sockets, instead you accept connections on them; accept returns the actual connection socket:
ser_sock.listen(1)
sock, addr = ser_sock.accept()
print('Got connection from {}'.format(addr))
# only this *connection* socket can receive!
data = sock.recv(4096)

Python: UDP Proxy Multithreading

I just can't manage to add Multithreading to my UDP Server.
A second client can connect, but instantly gets thrown out of the server when someone is already connected to it.
Could this be caused by something other than SingleThreading?
import sys, socket
localPort, remoteHost, remotePort = sys.argv[1].split(':')
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('', localPort))
except:
fail('Failed to bind on port ' + str(localPort))
knownClient = None
knownServer = (remoteHost, remotePort)
sys.stderr.write('Ready.\n')
while True:
data, addr = s.recvfrom(32768)
print addr
if knownClient is None:
knownClient = addr
if addr == knownClient:
try:
s.sendto(data, knownServer)
except:
pass
else:
try:
s.sendto(data, knownClient)
except:
pass
You cannot write an UDP proxy with only port. How should you know from the answer of the server to which of the two connected clients you should send your answer. You have to open for each client a new socket to the remote server.
It's not Python but "networking" and for sure not "multithreading". You need to either direct the clients to different ports or create a new outgoing socket for each new client connection.
Because you have multiple sockets a very effective approach is to sit on select and wait for the incoming calls.
In order to identify the clients, it is also needed to keep a reference of the local addresses the new sockets are using to talk to the server.
Your code reworked to open a socket on each new incoming client connection. No guarantees, because that would involve network testing against an scenario (yours) which is unknown.
For a very robust implementation you would have to add error checking, socket removal for gone connections ...
import select
import socket
import sys
localPort, remoteHost, remotePort = sys.argv[1].split(':')
try:
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.bind(('', localPort))
except:
fail('Failed to bind on port ' + str(localPort))
localaddr = s.getsockname() # (localhost, localport)
remaddr = (remoteHost, remotePort)
sys.stderr.write('Ready.\n')
allsockets = [server]
proxysocks = dict()
origins = dict()
while True:
toread, _, _ = select.select(allsockets, [], [])
s = toread[0] # 1st socket available to read
data, orig = s.recvfrom(32768) # data, (remhost,remport)
dest = s.getsockname() # (localhost, localport)
if dst == localaddr: # client -> localserver
try:
p = proxysocks[orig] # find proxy sock
except KeyError: # new client connection
proxysocks[orig] = p = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
proxyaddr = p.getsockname() # keep local address of new socket
origins[proxyaddr] = orig # link proxyaddr -> clientaddr
allsockets.append(p) # make it "selectable"
p.sendto(remaddr, data) # send to server
else: # server -> proxyaddr
s.sendto(origins[dstaddr])

How to make a simple multithreaded socket server in Python that remembers clients

How do I make a simple Python echo server that remembers clients and doesn't create a new socket for each request? Must be able to support concurrent access. I want to be able to connect once and continually send and receive data using this client or similar:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = raw_input("Server hostname or ip? ")
port = input("Server port? ")
sock.connect((host,port))
while True:
data = raw_input("message: ")
sock.send(data)
print "response: ", sock.recv(1024)
I.e. with the server running on port 50000, using the above client I want to be able to do this:
me#mine:~$ client.py
Server hostname or ip? localhost
Server Port? 50000
message: testa
response: testa
message: testb
response: testb
message: testc
response: testc
You can use a thread per client to avoid the blocking client.recv() then use the main thread just for listening for new clients. When one connects, the main thread creates a new thread that just listens to the new client and ends when it doesn't talk for 60 seconds.
import socket
import threading
class ThreadedServer(object):
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port))
def listen(self):
self.sock.listen(5)
while True:
client, address = self.sock.accept()
client.settimeout(60)
threading.Thread(target = self.listenToClient,args = (client,address)).start()
def listenToClient(self, client, address):
size = 1024
while True:
try:
data = client.recv(size)
if data:
# Set the response to echo back the recieved data
response = data
client.send(response)
else:
raise error('Client disconnected')
except:
client.close()
return False
if __name__ == "__main__":
while True:
port_num = input("Port? ")
try:
port_num = int(port_num)
break
except ValueError:
pass
ThreadedServer('',port_num).listen()
Clients timeout after 60 seconds of inactivity and must reconnect. See the line client.settimeout(60) in the function ThreadedServer.listen()

Categories