i am working on a basic python program to get used to threading and networking and i have become a little unstuck at one section of my code.
what i have is:
#make a socket and loop to obtain connections
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ads = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1" , 4000))
ads.bind(("127.0.0.1" , 4001))
s.listen(10)
ads.listen(1)
socks = [s,ads]
connections = [] # list of connections
while True:
if ads:
(c,a) = ads.accept()
t = threading.Thread(target = admin_client, args = ())
t.start()
elif :
(c,a) = s.accept()
connections.append(c)
t = threading.Thread(target = handle_client, args = (c,a))
t.start()
What i was hoping to happen was when the ads port was accessed it would assign it to the admin_client method which it seems to perform but it will just do nothing if anything connects on the s port.
Does anyone have a solution for this so both clients will connect with no issues?
if ads: is always True. You need to use select. Since if ads: is always True you drop into (c,a) = ads.accept() which waits for someone to connect to the ads port.
Something like (untested):
r,w,x = select.select(socks,[],[])
if ads in r:
...
elif s in r:
...
Related
Hello fellow programmer.
I followed this tutorial https://www.youtube.com/watch?v=QihjI84Z2tQ
Those server and client has successfully connected
but when i try build it did not show the desired output on the client-side terminal.
The server-side terminal does not react anything.
this is my code for server side:
import socket
import numpy as np
import encodings
HOST = '192.168.0.177' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
def random_data(): # ANY DATA YOU WANT TO SEND WRITE YOUR SENSOR CODE HERE
x1 = np.random.randint(0, 55, None) # Dummy temperature
y1 = np.random.randint(0, 45, None) # Dummy humidigy
my_sensor = "{},{}".format(x1,y1)
return my_sensor # return data seperated by comma
def my_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("Server Started waiting for client to connect ")
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024).decode('utf-8')
if str(data) == "Data":
print("Ok Sending data ")
my_data = random_data()
x_encoded_data = my_data.encode('utf-8')
conn.sendall(x_encoded_data)
elif str(data) == "Quit":
print("shutting down server ")
break
if not data:
break
else:
pass
if __name__ == '__main__':
while 1:
my_server()
and this is my client code:
import socket
import threading
import time
HOST = '192.168.0.177' # The server's hostname or IP address
PORT = 65432 # The port used by the server
def process_data_from_server(x):
x1, y1 = x.split(",")
return x1,y1
def my_client():
threading.Timer(11, my_client).start()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
my = input("Data")
my_inp = my.encode('utf-8')
s.sendall(my_inp)
data = s.recv(1024).decode('utf-8')
x_temperature,y_humidity = process_data_from_server(data)
print("Temperature {}".format(x_temperature))
print("Humidity {}".format(y_humidity))
s.close()
time.sleep(5)
if __name__ == "__main__":
while 1:
my_client()
I have tried many solution by printing "Data" directly to the terminal.
can anyone help me?
Ok, I have found the problem. I am using Sublime Text 3 when running the client.py script When i post in the build it doesnt response nothing. So I change my IDE to PYCharm and then it worked. I don't know why. I hope that's helpful to other people that have this problem. Thank you very much.
my raspberry pi is the server and Im trying to send continuous message from rpi to android while recieving a command from client (android app),i really dont know if this is possible and how to do it is out of my reach and it is not a feedback message here is my code hope you will help me thank you.
import apptopi
from socket import *
from time import ctime
from nanpy import (ArduinoApi, SerialManager)
apptopi.setup()
connection = SerialManager()
a = ArduinoApi(connection = connection)
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
add = 0
add += 1
a = str(add) //**this is a sample that i want to send continously
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(0)
tcpSerSock.send(str.encode(a)) <== i really don't know how to send
continuously
while True:
print 'Waiting for connection'
tcpCliSock,addr = tcpSerSock.accept()
print '...connected from :', addr
try:
while True:
data = ''
data = tcpCliSock.recv(BUFSIZE)
if not data:
break
if data == ctrCmd[0]:
apptopi.forw()
print 'forward'
if data == ctrCmd[1]:
apptopi.back()
print 'backward'
if data == ctrCmd[2]:
apptopi.left()
print 'leftturn'
if data == ctrCmd[3]:
apptopi.right()
print 'rightturn'
if data == ctrCmd[4]:
apptopi.stp()
print 'stop'
except KeyboardInterrupt:
apptopi.close()
GPIO.cleanup()
tcpSerSock.close();
OK one approach is to use the select() function for this. There is information in the documentation about its operation.
As an example I've made a modified version of your program (see below). I don't have a raspberry pi, so that part of the code is commented out, but you can replace it as needed.
The example uses the timeout feature of select() to send "continuous" messages to clients whilst also monitoring them for incoming messages. You can adjust the message contents and timeout to whatever works for you. NB you may also need to respond to client messages, as this code only sends data to clients after a timeout. Make whatever changes you need.
import sys
import socket
import select
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(1)
print 'Waiting for connection'
sendInterval = 1.0 # interval(sec) for sending messages to connected clients
rxset = [tcpSerSock]
txset = []
while 1:
rxfds, txfds, exfds = select.select(rxset, txset, rxset, sendInterval)
if rxfds:
for sock in rxfds:
if sock is tcpSerSock:
# a client is connecting
tcpCliSock, addr = tcpSerSock.accept()
tcpCliSock.setblocking(0)
rxset.append(tcpCliSock)
print '...connected from :', addr
else:
# a client socket has data or has closed the connection
try:
data = sock.recv(BUFSIZE)
if not data:
print "...connection closed by remote end"
rxset.remove(sock)
sock.close()
else:
if data == ctrCmd[0]:
#apptopi.forw()
print 'forward'
if data == ctrCmd[1]:
#apptopi.back()
print 'backward'
if data == ctrCmd[2]:
#apptopi.left()
print 'leftturn'
if data == ctrCmd[3]:
#apptopi.right()
print 'rightturn'
if data == ctrCmd[4]:
#apptopi.stp()
print 'stop'
except:
print "...connection closed by remote end"
rxset.remove(sock)
sock.close()
else:
# timeout - send data to any active client
for sock in rxset:
if sock is not tcpSerSock:
sock.send("Hello!\n")
The simple client program I used to test this is here:
import sys
import socket
import time
ctrCmd = ['Up','Down','Left','Right','Stop','Connect']
HOST = '127.0.0.1'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST,PORT)
tcpCliSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpCliSock.connect(ADDR)
time.sleep(1)
for i in range(len(ctrCmd)):
tcpCliSock.send(ctrCmd[i])
time.sleep(1)
data = tcpCliSock.recv(BUFSIZE)
print data
tcpCliSock.close()
Hope this helps, best of luck.
I'm currently working on a project and I need to use socket (python). My problem is :
- When the client disconnect, my server disconnect too. But I don't want this. I want the server to stay alive all the time, how can I do that ?
If I close the client I would like the server to keep alive
Here is my code :
Client :
import socket
hote = "localhost"
port = 12800
connexion_avec_serveur = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_avec_serveur.connect((hote, port))
print("Established {}".format(port))
msg_a_envoyer = b""
while msg_a_envoyer != b"fin":
msg_a_envoyer = input("> ")
msg_a_envoyer = msg_a_envoyer.encode()
connexion_avec_serveur.send(msg_a_envoyer)
msg_recu = connexion_avec_serveur.recv(1024)
print(msg_recu.decode())
print("Close connection")
connexion_avec_serveur.close()
Server :
import socket
import select
hote = ''
port = 12800
connexion_principale = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connexion_principale.bind((hote, port))
connexion_principale.listen(5)
print("rece {}".format(port))
serveur_lance = True
clients_connectes = []
while serveur_lance:
connexions_demandees, wlist, xlist = select.select([connexion_principale],
[], [], 0.05)
for connexion in connexions_demandees:
connexion_avec_client, infos_connexion = connexion.accept()
clients_connectes.append(connexion_avec_client)
clients_a_lire = []
try:
clients_a_lire, wlist, xlist = select.select(clients_connectes,
[], [], 0.05)
except select.error:
pass
else:
for client in clients_a_lire:
msg_recu = client.recv(1024)
msg_recu = msg_recu.decode()
print("Recu {}".format(msg_recu))
client.send(b"5 / 5")
if msg_recu == "fin":
serveur_lance = False
Excuse my english.
Thanks for your help
I suspect it's not when the client disconnects, as such, but when the client sends "fin". That's when the client's loop stops, and also when the server's loop stops. The only thing in the code you've posted that changes the value of serveur_lance is when the client sends "fin":
while serveur_lance:
...
if msg_recu == "fin":
serveur_lance = False
So the moment someone types fin into the client, the server will also get the command to stop.
Instead of changing the value of serveur_lance in your server code, you probably want to close that specific connection and remove it from the clients_connectes list.
My current code is as follows:
from socket import *
import threading
import datetime
import csv
import time
global clist
clist = []
HOST = "ip"
PORT = 1876
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
def clientHandler():
print("Thread successful\n")
connect, address = s.accept()
clist.append(connect)
iplist = open("iplist.txt","a+")
iplist.write(str(address)+"\n")
iplist.close()
print('IP Recorded.')
while 1:
try:
data = connect.recv(1024)
data = data.decode()
current_date = time.strftime("%d-%m")
log = open(current_date+"log.txt","a+")
print("("+time.strftime("%H:%M:%S")+")","> ", data)
log.write("> "+data+"\n")
log.close()
data = data.encode()
for item in clist:
item.sendall(data)
except: #handles clients leaving and fixes reconnect bug, succesfully removing them from clist
clist.remove(connect)
print(address, 'has disconnected.')
break
print("Server started...")
for i in range(5):
makeThread = threading.Thread(target=clientHandler)
makeThread.start()
aliveList = threading.enumerate()
print(aliveList)
My problem is that once clients join or rejoin (x5) the server won't allow any more connections (because the threads of my clientHandler function have been used). Would I have to close the threads and some how make them reusable to allow more clients to join/rejoin? (How would I do this?)
Also, could anybody recommend improvements to my code/guide me on the right path with socket programming, as I am very new to all this.
All help appreciated,
cheers.
I suggest using threading.activeCount() to enumerate your threads.
Lets say you want 5 clients max , then activeCount() should be <= 6 ( five clients and the main thread )
Try this loop :
while True :
try :
if threading.activeCount() < 6 :
makeThread = threading.Thread(target=clientHandler)
#makeThread.daemon = True ## uncomment this line if you want your threads to terminate when the main thread dies ##
makeThread.start()
print('Active clients: {}'.format(threading.activeCount() - 1))
except KeyboardInterrupt :
print('terminated')
break
Now when clientHandler() returns , there is room for the next client .
I'm pretty new to the Python, trying to write a code to receive string from UDP connection, the problem I have now is that I need to receive data from 2 sources, I want the program continue looping if there is no data from either or both of them, but now if there is no data from source 2, it will stop there and wait for the data, how to solve it?
I was thinking about using if statement, but I don't know how to check if the incoming data is empty of not, any ideas will be appreciated!
import socket
UDP_IP1 = socket.gethostname()
UDP_PORT1 = 48901
UDP_IP2 = socket.gethostname()
UDP_PORT2 = 48902
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind((UDP_IP1, UDP_PORT1))
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2.bind((UDP_IP2, UDP_PORT2))
while True:
if sock1.recv != None:
data1, addr = sock1.recvfrom(1024)
data1_int = int(data1)
print "SensorTag[1] RSSI:", data1_int
if sock2.recv != None:
data2, addr = sock2.recvfrom(1024)
data2_int = int(data2)
print "SensorTag[2] RSSI:", data2_int
If select doesn't work out for you you can always throw them into a thread. You'll just have to be careful about the shared data and place good mutex around them. See threading.Lock for help there.
import socket
import threading
import time
UDP_IP1 = socket.gethostname()
UDP_PORT1 = 48901
UDP_IP2 = socket.gethostname()
UDP_PORT2 = 48902
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind((UDP_IP1, UDP_PORT1))
sock2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock2.bind((UDP_IP2, UDP_PORT2))
def monitor_socket(name, sock):
while True:
sock.recv is not None:
data, addr = sock.recvfrom(1024)
data_int = int(data)
print(name, data_int)
t1 = threading.Thread(target=monitor_socket, args=["SensorTag[1] RSSI:", sock1
t1.daemon = True
t1.start()
t2 = threading.Thread(target=monitor_socket, args=["SensorTag[2] RSSI:", sock2])
t2.daemon = True
t2.start()
while True:
# We don't want to while 1 the entire time we're waiting on other threads
time.sleep(1)
Note this wasn't tested due not having two UPD sources running.