I created a TCP server and set the timeout to 30s but i want the timeout countdown to show on the screen in realtime and print Client not connected if the 30 elapsed.
Expected output:
import socket
#import datetime
import time
def countdown(t):
while t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.format(mins, secs)
print(timer, end="\r")
time.sleep(1)
t -= 1
t = 30
#countdown(t)
def SocketServer():
host = input ("Enter the server IP addresss:" )
port = 8888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
s.listen(2)
s.settimeout(t)
print(""" ***********************************************************
This Server will timeout if no client connect in 30 seconds
************************************************************""")
countdown(t)
conn, addr = s.accept()
print(addr, "connectiom is establish")
conn.send("Hello Client!".encode())
conn.close()
except socket.timeout:
print("client not connected!")
s.close()
SocketServer()
I hope this will help you!
I will post the code for a client to connect with this server!
I wrote some lines in order to guide you!
import socket
import threading
import time
class SocketServer():
def __init__(self):
self.t=30
self.adr=""
print(""" ***********************************************************
This Server will timeout if no client connect in 30 seconds
************************************************************""")
#let's create a new thread for out server
# in the main thread, we will print and count seconds....
self.thread=threading.Thread(target=self.thread)
self.thread.daemon=True
self.thread.start()
self.counter()
def thread(self):
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 8888 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
#we want to know if someone is connected
#by set self.adr with something that has len>0 we will informe the main thread that it needs to stop
self.adr="Connected"
conn.send("Hello Client!".encode())
s.close() #if you want to close, or leave it to further connections
def counter(self):
for x in range(self.t + 1):
time.sleep(1)#sleep function
if (len(self.adr)!=0): #every time we chech if sel.adr is different from 0
print("Connection is established")
print("Connected after 00:{:02d}".format(x))
break #stop
else:
print ('00:{:02d}'.format(x))
#host = raw_input("Enter the server IP addresss:")
SocketServer()
Code for the client!
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 8888 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Output:
***********************************************************
This Server will timeout if no client connect in 30 seconds
************************************************************
00:00
00:01
00:02
00:03
Connected by ('127.0.0.1', 27191)
Connection is established
Connected after 00:04
Client output:
Received b'Hello Client!'
Related
I cant figure out why the client is not communicating with the Server (2 seperate jupyter notebooks with own kernel), no error messages - any suggestions?
the following code is working (without multiprocessing):
Server:
import socket
HOST = '127.0.0.1'
PORT = 50000
def start_server():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
print ('waiting for client')
conn, addr = s.accept()
print('client connected #', addr[0], 'port:', addr[1])
return conn
Server0 = start_server()
Client:
import socket
HOST = '127.0.0.1'
PORT = 50000
data = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect_ex((HOST, PORT))
while True:
byte_message = s.recv(1024)
string_message = byte_message.decode('utf8')
header, time, data = string_message.split(' ')
if header == 'close':
s.shutdown(socket.SHUT_RDWR)
s.close()
print('client connection shut down')
header = ''
elif header == '<<':
print(time,float(data))
else:
pass
As soon as I pack the client into a function and run it as a process, client and server do not connect anymore(no error is given, and the server doesent get to this line:
print('client connected #', addr[0], 'port:', addr[1])
not working client:
import socket
import multiprocessing
HOST = '127.0.0.1'
PORT = 50000
def client():
data = []
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect_ex((HOST, PORT))
while True:
byte_message = s.recv(1024)
string_message = byte_message.decode('utf8')
header, time, data = string_message.split(' ')
if header == 'close':
s.shutdown(socket.SHUT_RDWR)
s.close()
print('client connection shut down')
header = ''
elif header == '<<':
print(time,float(data))
else:
pass
client_process = multiprocessing.Process(target= client)
client_process.start()
Im trying to implement both TCP and UDP in my server. I can accept a TCP or UDP port connection from a client.
for example, I want to have code to accept TCP and UDP in one program:
# create a socket
sockTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #TCP
sockUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
# server address to connect to
server_addressTCP = ('localhost', tcp_port)
server_addressUDP = ('localhost', udp_port)
# bind socket to adress and port number
sockTCP.bind(server_addressTCP)
sockUDP.bind(server_addressUDP)
# wait for connections (clients)
print("Waiting for connections...")
print(" ")
sockTCP.listen(20)
request = ''
while True:
#TCP
client_sock, client_addr = sockTCP.accept()
data, addr = client_sock.recvfrom(1024)
#UDP
udp_data, udp_addr = sockUDP.recvfrom(1024)
# DO SOMETHING WITH DATA.........
client_sock.close()
If you want to implement a server that listens on UDP and TCP then create listener threads: one for UDP and one for TCP connections.
Here is an example of a server listening on both TCP and UDP ports.
import socket
import threading
tcp_port = udp_port = 1234
def udp_listen(conn):
while True:
data = conn.recv(1024)
if len(data) != 0:
print("recv:", data)
def tcp_listen(sock):
while True:
conn, _ = sock.accept()
try:
while True:
data = conn.recv(1024)
if len(data) == 0:
break
print("recv:", data)
except Exception:
pass
try:
conn.close()
except Exception:
pass
# create a socket
sockTCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP
sockUDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
# server address to connect to
server_addressTCP = ('localhost', tcp_port)
server_addressUDP = ('localhost', udp_port)
# bind socket to adress and port number
sockTCP.bind(server_addressTCP)
sockUDP.bind(server_addressUDP)
sockTCP.listen(20)
t1 = threading.Thread(target=tcp_listen, args=(sockTCP,))
t2 = threading.Thread(target=udp_listen, args=(sockUDP,))
print("Waiting for connections...")
t1.start()
t2.start()
t1.join()
t2.join()
I'm using threads to have a server listening for both TCP and UDP messages. Here is the code:
from threading import Thread
import time
import socket
Listening_Port = 5005
Listening_IP = "127.0.0.1"
#define UDP listening function
def UDPListen():
BUFFER_SIZE = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # IPv4, UDP
sock.bind((Listening_IP, Listening_Port))
while True:
data, address = sock.recvfrom(BUFFER_SIZE)
print "UDP Messsage from address: ", address
print "Message: ", data
#define a TCP listening function
def TCPListen():
BUFFER_SIZE = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPv4, TCP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((Listening_IP, Listening_Port))
while True:
sock.listen(1)
conn, address = sock.accept()
print "TCP connection from", address
data = conn.recv(BUFFER_SIZE)
print "Mesage: ", data
conn.close()
# main function
def main():
ThreadUDP = Thread(target=UDPListen)
ThreadTCP = Thread(target=TCPListen)
print "Starting Server..."
ThreadUDP.start()
ThreadTCP.start()
print "Server Started!"
if __name__ == "__main__":
main()
The problem is that when I press ctrl + c (even multiple times), the program is not terminated and I should close the console.
I tried something like this for def main(), but it didn't work:
def main():
ThreadUDP = Thread(target=UDPListen)
ThreadTCP = Thread(target=TCPListen)
try:
print "Starting Server..."
ThreadUDP.start()
ThreadTCP.start()
print "Server Started!"
# Hit Break / Ctrl-C to exit
except KeyboardInterrupt:
print('\nClosing')
raise
Updated code according to the solution offered in the answers:
from threading import Thread
import time
import socket
Listening_Port = 5005
Listening_IP = "10.0.0.3"
#define UDP listening function
def UDPListen():
BUFFER_SIZE = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # IPv4, UDP
sock.bind((Listening_IP, Listening_Port))
while not eve.isSet():
data, address = sock.recvfrom(BUFFER_SIZE)
print "UDP Messsage from address: ", address
print "Message: ", data
#define a TCP listening function
def TCPListen():
BUFFER_SIZE = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPv4, TCP
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((Listening_IP, Listening_Port))
while not eve.isSet():
sock.listen(1)
conn, address = sock.accept()
print "TCP connection from", address
data = conn.recv(BUFFER_SIZE)
print "Mesage: ", data
conn.close()
# main function
def main():
ThreadUDP = Thread(target=UDPListen)
ThreadTCP = Thread(target=TCPListen)
eve = threading.Event()
print "Starting Server..."
ThreadUDP.start()
ThreadTCP.start()
print "Server Started!"
try:
while True:
eve.wait(2)
except Exception:
eve.set()
if __name__ == "__main__":
main()
but I received an error:
NameError: Global name 'threading' is not defined
the problem you have is that you run your listeners in a thread - meaning that signals caught in the main should somehow signal the threads.
use threading.Event and then in the main function :
eve = threading.Event()
<start your threads>
try:
while True:
eve.wait(2)
except Exception
eve.set()
and in the threads instead of while True use while not eve.isSet():
Just started learning python socket programming. I'm trying to send and receive data from a Raspberry Pi 3 using python sockets. I'm running the server program on the Raspberry Pi and the client program on my laptop running Ubuntu. Both are on the same WiFi network. The program runs and the client connects to the server, but there is no data transfer. Both the client and server do nothing. The two programs run properly if I try to run them both on the same device. Here's my code :-
Client
import socket
HOST = '172.16.31.51'
PORT = 5000
BUFSIZ = 1024
if __name__ == '__main__':
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input("Enter hostname [%s]: " %HOST) or HOST
port = input("Enter port [%s]: " %PORT) or PORT
sock_addr = (host, int(port))
client_sock.connect(sock_addr)
payload = 'GET TIME'
try:
while True:
client_sock.send(payload.encode('utf-8'))
data = client_sock.recv(BUFSIZ)
print(repr(data))
more = input("Want to send more data to server[y/n]:")
if more.lower() == 'y':
payload = input("Enter payload: ")
else:
break
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
Server
import socket
from time import ctime
PORT = 5000
BUFSIZ = 1024
ADDR = ('', PORT)
if __name__ == '__main__':
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(ADDR)
server_socket.listen(5)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
while True:
print('Server waiting for connection...')
client_sock, addr = server_socket.accept()
print('Client connected from: ', addr)
while True:
data = client_sock.recv(BUFSIZ)
if not data or data.decode('utf-8') == 'END':
break
print("Received from client: %s" % data.decode('utf-8'))
print("Sending the server time to client: %s" %ctime())
try:
client_sock.send(bytes(ctime(), 'utf-8'))
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
server_socket.close()
EDIT:
The screenshots - https://imgur.com/a/NgzsC
As soon as I hit Ctrl + C, on the client side, the server seems to send some data before it disconnects from the client. Here's a screenshot of that - https://imgur.com/a/hoLwN
Your sockets work OK, but you made some other programming mistakes. Did you ever run it?
This is the modified working code.
(Hint: you can start testing just the client when you use nc -l 5000 as a simple manual server.
Client
import socket
HOST = '127.0.0.1'
PORT = 5000
BUFSIZ = 1024
if __name__ == '__main__':
client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = str(raw_input("Enter hostname [%s]: " %HOST)) or HOST
port = str(raw_input("Enter port [%s]: " %PORT)) or PORT
sock_addr = (host, int(port))
client_sock.connect(sock_addr)
payload = 'GET TIME'
try:
while True:
client_sock.send(payload.encode('utf-8'))
data = client_sock.recv(BUFSIZ)
print(repr(data))
more = raw_input("Want to send more data to server[y/n]:")
if more.lower() == 'y':
payload = str(raw_input("Enter payload: "))
else:
break
except KeyboardInterrupt:
print("Exited by user")
client_sock.close()
Server
import socket
from time import ctime
PORT = 5004
BUFSIZ = 1024
ADDR = ('', PORT)
if __name__ == '__main__':
server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server_socket.bind(ADDR)
server_socket.listen(5)
server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR, 1)
while True:
print('Server waiting for connection...')
client_sock, addr = server_socket.accept()
print('Client connected from: ', addr)
while True:
data = client_sock.recv(BUFSIZ)
if not data or data.decode('utf-8') == 'END':
break
print("Received from client: %s" % data.decode('utf-8'))
print("Sending the server time to client: %s" %ctime())
try:
client_sock.send( ctime().encode('utf-8') )
except KeyboardInterrupt:
print("Exited by user")
break;
client_sock.close()
server_socket.close()
I have implemented a small benchmark for socket communication via TCP and UDP in Python. Surprisingly, TCP is almost exactly double as fast as UDP.
To avoid routing effects, server and client are running on the same Unix machine, but on different threads.
Maybe the code is useful. Here is the server code:
import socket
import sys
host = 'localhost'
port = 8888
buffersize = 8
server_address = (host, port)
def start_UDP_server():
socket_UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_UDP.bind(server_address)
print("UDP server is running...")
while True:
data, from_address = socket_UDP.recvfrom(buffersize)
if not data: break
socket_UDP.sendto(data, from_address)
socket_UDP.close()
def start_TCP_server():
socket_TCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_TCP.bind(server_address)
socket_TCP.listen(1)
print("TCP server is running...")
while True:
client, client_address = socket_TCP.accept()
while True:
data = client.recv(buffersize)
if not data: break
client.sendall(data)
client.close()
So you can run either start_TCP_server() or start_UDP_server().
On client side the code is:
import socket
import sys
import time
host = 'localhost'
port = 8888
buffersize = 8
server_address = (host, port)
client_address = (host, port+1)
N = 1000000
def benchmark_UDP():
socket_UDP = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
socket_UDP.bind(client_address)
print("Benchmark UDP...")
duration = 0.0
for i in range(0, N):
b = bytes("a"*buffersize, "utf-8")
start = time.time()
socket_UDP.sendto(b, server_address)
data, from_address = socket_UDP.recvfrom(buffersize)
duration += time.time() - start
if data != b:
print("Error: Sent and received data are bot the same")
print(duration*pow(10, 6)/N, "µs for UDP")
def benchmark_TCP():
socket_TCP = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_TCP.connect(server_address)
print("Benchmark TCP...")
duration = 0.0
for i in range(0, N):
b = bytes("a"*buffersize, "utf-8")
start = time.time()
socket_TCP.sendall(b)
data = socket_TCP.recv(buffersize)
duration += time.time() - start
if data != b:
print("Error: Sent and received data are bot the same")
print(duration*pow(10, 6)/N, "µs for TCP")
socket_TCP.close()
Like for the server you can start the benchmark by benchmark_TCP() or benchmark_UDP().
The results are about 25 µs for TCP, and about 54 µs for UDP on Unix and even worse for Windows (about 30 µs for TCP and more than 200 µs for UDP). Why? I would expect a minimal advantage for UDP.
Your TCP socket is connected but your UDP socket is not. This means extra processing for every send/receive on the UDP socket. Call connect on each side for the UDP socket, just like you call connect/accept on the TCP socket.
Programs like iperf do this to measure accurately.