Hello I've been trying to make my python sock server connection counter to go down
but I can't figure out how I can do this
def client_thread(conn):
while True:
conn.send("Command: ")
data = conn.recv(1024)
if not data:
break
reply = "" + data
conn.sendall("\r")
if data == "!* Connections":
conn.sendall("[+] Clients Connected: %s \r\n" % (clients))
conn.close()
while True:
conn, addr = sock.accept()
clients = clients + 1
start_new_thread(client_thread, (conn,))
sock.close()
I needn't show you all of the code because its irrelevant to this issue,
I have provided the code that make's the counter go up when a new connection connects, but as said before I don't know how to make it go down when a connection leaves.
When trying to find solutions online there show's nothing that can help my issue
Here is a small sample how to realize a client counter with the select.select function. I actually took it from the great article select – Wait for I/O Efficiently on pymotw.com and added a client counter. Basically you look for readable sockets and try to receive data from them. If a socket returns nothing it means it has been closed and can be removed from the client list.
import queue
import socket
import select
clients = 0
sock = socket.socket()
sock.bind(('localhost', 5000))
sock.listen(5)
inputs = [sock]
outputs = []
msg_queues = {}
while inputs:
readable, writable, exceptional = select.select(
inputs, outputs, msg_queues)
for s in readable:
if s is sock:
conn, addr = sock.accept()
print('new connection from ', addr)
conn.setblocking(0)
inputs.append(conn)
msg_queues[conn] = queue.Queue()
# increment client counter
clients += 1
print('Clients: ', clients)
else:
# try to receive some data
data = s.recv(1024)
if data:
# if data available print it
print('Received {} from {}'.format(data, s.getpeername()))
msg_queues[s].put(data)
# add output channel for response
if s not in outputs:
outputs.append(s)
else:
# empty data will be interpreted as closed connection
print('Closing connection to ', s.getpeername())
# stop listening for input on the connection
if s in outputs:
outputs.remove(s)
# remove from inputs
inputs.remove(s)
s.close()
# decrement client counter
clients -= 1
del msg_queues[s]
print('Clients: ', clients)
Related
I know the code to wait for socket in a loop like this.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
data, addr = s.recvfrom(1024)
data = data.decode('utf-8')
print("Message from: " + str(addr))
print("From connected user: " + data)
com = data
data = data.upper()
data = "Message Received: " + data
however, I want to use this function in another main loop that refreshes every second. When this get called it freezes until meeting any msg.
Is there any way that I can "check msg" on off, and integrate this in the main loop refreshes every second?
Many thanks.
You can use select.select to poll for readiness to receive a message:
import socket
import select
s = socket.socket(type=socket.SOCK_DGRAM)
s.bind(('', 5000))
while True:
r, _, _ = select.select([s], [], [], 1.0)
if r: # r will contain s if a message is ready
data, addr = s.recvfrom(1024)
data = data.decode('utf-8')
print(f'\n{addr}: {data}')
else:
print('.', end='', flush=True) # show activity
I am attempting to complete a challenge that states:
We've noticed that the aliens are sending messages between their ships, we think they're using XOR to encrypt the messages, and we've intercepted a key.
Set up a server listening on ("localhost", 10000) to intercept one of the alien messages. When you do perform a bitwise XOR on the message with the key "attackthehumans" and then respond with the encrypted data.
Tip: Read the response to get the flag.
After some research, I was able to come up with the following code. However, when I run it in the challenge's code editor, the only feedback I receive is "Error trying to connect to your server and recieving message back."
import socket
# Function to xor strings
def xor_string(string):
key = "attackthehumans"
bit_key = ''.join(format(ord(i), 'b') for i in key)
bit_data = ''.join(format(ord(i), 'b') for i in string)
xor_string = str(0)
for i in range(len(bit_key)):
if bit_data[i] == bit_key[i]:
xor_string = xor_string + str(0)
else:
xor_string = xor_string + str(1)
return xor_string
# Sets up server on localhost and port 10000
print("Setting up server...")
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("localhost", 10000))
print("Server set up. Listening for connections...")
server.listen()
conn, addr = server.accept()
print("Connected by: {}".format(addr))
# Once connection is established, server receives data, XOR's it, sends it back, and
# waits for response to get the flag.
with conn:
print("Receiving data from connection... ")
data = conn.recv()
data = data.decode()
print("Data received: {}".format(data.decode()))
xor_data = xor_string(data)
conn.sendall(xor_data.encode())
response = conn.recv()
response = response.decode()
print(response)
I am not sure what I'm doing wrong. I can't figure out if it's the socket or the xor_string function. Any help would be appreciated.
This should be enough to do your XORing:
def xor_string(string):
key = b"attackthehumans"
key = key * (len(string)//len(key)+1)
res = bytes([k^s for (k,s) in zip(key,string)])
return res
Then your main code becomes:
print("Receiving data from connection... ")
data = conn.recv()
print("Data received:", data)
xor_data = xor_string(data)
conn.sendall(xor_data)
print(conn.recv())
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.
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 .
In the code shown below I am using the blocking call to receive 50 bytes of data from socket and echo it back.But what is happening is that the code stuck after receiving one byte.In the telnet running on another command prompt the connection still shows as connected. What might be missing from this ?
import socket
import sys
host = ''
port = 8888
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
print 'Socket Bind Complete'
s.listen(10)
print 'Now Listening'
while 1:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
The problem is that you're accepting a new connection each time through the loop, and only receiving from that connection once. The next time through the loop, your forget about that connection and accept a new one, which blocks until someone else connects.
If you just want to handle a single connection and quit, just move the accept outside the loop:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
If you want to handle one connection at a time, but then wait for a new connection after you finish with the first, add an outer loop.
while True:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
If you want to handle more than one connection at a time, as most servers do, you need some sort of asynchronous mechanism—select and nonblocking sockets, gevent, threads, whatever. For example:
def handle_client(conn, addr):
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
client_threads = []
try:
while True:
conn, adr = s.accept()
client_thread = threading.Thread(target=handle_client, args=[conn, addr])
client_thread.start()
client_threads.append(client_thread)
finally:
s.close()
for client_thread in client_threads:
client_thread.join()
In any of these designs, you're probably better off using a with statement instead of explicit close calls and/or try/finally.