What do commas in Python 2.X do when declaring a variable? - python

I am using some code out of a book for NetSec, but there is one line I cannot figure out. I am knowledgeable of Python 3, but not 2, which is what this book is eccentric upon.
The code is:
client,addr = server.accept()
To be quite frank, what the hell does this mean? The entire code for the project is here:
import socket
import threading
ip = "192.168.0.155"
port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((ip, port))
server.listen(5)
print "* Listening on %s:%d" %(ip,port)
def handle_client(client_socket):
request = client_socket.recv(1024)
print "* Received %s" % request
client,addr = server.accept()
client_socket.send("Received: %s" % request)
client,addr = server.accept()
client_socket.send("Received: %s" % request)
client_socket.close()
for each in request:
print each
while True:
client,addr = server.accept()
print "* Received connection from %s:%d" % (addr[0], addr[1])
client_handler = threading.Thread(target=handle_client, args=(client,))
client_handler.start()

That's called sequence unpacking. server.accept() returns a tuple (socket, address), which is unpacked into the two variables client and addr.
It's equivalent to
temp= server.accept()
client= temp[0]
addr= temp[1]
More info in the docs.

server.accept() is returning a tuple, and the values of that tuple are being assigned to client and addr respectively.
A simplified example:
> x,y = (1,2)
> print(x)
1
> print(y)
2
This is a very common idiom in Python, and I don't think it's any different in Python 3.
Edit
Regarding the trailing comma in ...args=(client,))
see here: Python tuple trailing comma syntax rule

Related

socket is not listening for multiple request

Here is the code for server -
import socket, select,re
def getSocket( idd):
return CONNECTION_LIST[idd]
def broadcast_data (sock, message):
for socket in CONNECTION_LIST:
if socket != server_socket and socket != sock :
try :
socket.send(message)
except :
socket.close()
CONNECTION_LIST.remove(socket)
def single_client (sock , message , idd):
socket = getSocket ( idd )
if socket :
socket.send(message)
else:
print "chudap"
if __name__ == "__main__":
CONNECTION_LIST = []
RECV_BUFFER = 4096
PORT = 5000
PORTC = 2225
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("0.0.0.0", PORT))
server_socket.listen(10)
listen = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
listen.bind(("0.0.0.0" , PORTC))
#listen.listen(10)
CONNECTION_LIST.append(server_socket)
CONNECTION_LIST.append(listen)
print "Chat server started on port " + str(PORT)
idd = 1
while 1:
# Get the list sockets which are ready to be read through select
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
for sock in read_sockets:
if sock == server_socket:
sockfd, addr = server_socket.accept()
CONNECTION_LIST.append(sockfd)
#name = sockfd.recv(RECV_BUFFER)
print "connected from ip %s, id assigned is %d" % (addr[0] , idd)
broadcast_data(sockfd, "client with IP %s has entered with id = %d\n" % (addr[0] , idd))
idd += 1
elif sock == listen:
print "debugging"
data,addr = listen.recvfrom(RECV_BUFFER)
print "Received server probe request from [%s:%s]"%addr
listen.sendto("iam" , addr)#(addr[0] , 2624))
listen.close()
CONNECTION_LIST.remove(listen)
listen = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
listen.bind(("0.0.0.0" , PORTC))
CONNECTION_LIST.append(listen)
else:
try:
data = sock.recv(RECV_BUFFER)
if re.findall(r'.*/msg\d+' , data):
#print "got single client message request" + data
name = "private message from " + re.findall('([^:]+): /msg(\d+)([^"]+)' , data)[0][0] + ": "
#print name
eid = int(re.findall('([^:]+): /msg(\d+)([^"]+)' , data)[0][1])
#print eid
data = re.findall('([^:]+): /msg(\d+)([^"]+)' , data)[0][2]
#print data
data = name + data
#print "single client message sent with id = %d" %eid
single_client( sock , data , int(eid))
elif data:
broadcast_data(sock, data)
except:
broadcast_data(sock, "Client (%s, %s) is offline" % addr)
print "Client (%s, %s) is offline" % addr
sock.close()
CONNECTION_LIST.remove(sock)
continue
server_socket.close()
Here is the code for client -
import socket, select, string, sys
def prompt() :
sys.stdout.write('<You>: ')
sys.stdout.flush()
def exit(sock):
print "\n Thank you for using chat application\nBye"
sock.close()
sys.exit()
def printUsage():
print "1. By default your message will be sent to all clients sitting on the chat server"
print "2. You can send a private message to a person by starting your message as \"/msg{id}{Your message}\" for example /msg2Hi will send \"hi\" to client with id 2"
print "3. For quitting simply type \"/q\" or \"/quit\""
prompt()
PORTS = 2225
PORTC = 2624
if __name__ == "__main__":
broad = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
broad.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
broad.bind(( '0.0.0.0' , 2624) )
broad.sendto(b'whoisserver', 0, ("255.255.255.255", PORTS))
broad.settimeout(10)
print 15*"-" + "WELCOME TO CHATVILLE" + 15*"-" + "\nFinding the server"
try:
data , addr = broad.recvfrom(10)
except:
print "Can't find server ! Please ensure that server is up"
broad.close()
sys.exit()
broad.close()
if data <> "iam":
print "Can't find a valid server !"
sys.exit()
host = addr[0]
port = 5000
print addr
# host = sys.argv[1]
# port = int(sys.argv[2])
# print host,port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
name = raw_input("Please Enter your name: ")
try :
s.connect((host, port))
s.send(name)
except :
print 'Unable to connect'
sys.exit()
print 'Connected to remote host. Enjoy...............................'
name = "<" + name + ">" + ": "
print " - type /h to see usage instructions any time :) - "
prompt()
while 1:
socket_list = [sys.stdin, s]
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
if sock == s:
data = sock.recv(4096)
if not data :
print '\nDisconnected from chat server'
sys.exit()
else :
print ""
sys.stdout.write(data)
prompt()
else :
msg = sys.stdin.readline()
if str.startswith(msg, "/h") or str.startswith(msg,"/help"):
printUsage()
elif str.startswith(msg, "/quit") or str.startswith(msg,"/q"):
exit(s)
else:
msg = name + msg
s.send(msg)
prompt()
Main problem is that only one client is able to connect as soon as the first client connects are to server no other client is able to discover the server.
I tried by looking at the client's code by tcpdump and I can see the packet going at port number 2225, but the socket listen is not responding at all after the first connection.
PS - earlier I was not making instance of listen socket again and again but I tried this also and it didn't work out.
In the sever broadcast_data() does not exclude the UDP socket (listen) from the sockets to write to, and so it calls send() on that socket. This fails with exception
socket.error: [Errno 89] Destination address required
because no address is supplied (and can't be with socket.send()). The exception handler then closes the UDP socket and no further messages from new clients can be received. That's why additional clients can not connect to the server.
This is a perfect example of why it is not a good idea to use a bare except, i.e. an except statement that handles all exceptions. In this case the handler closes the UDP socket without even logging the fact. There are other instances of bare except statements in your code. I suggest that you handle specific exceptions to avoid this sort of bug. You can fix it by adding the UDP socket to the list of sockets to ignore:
def broadcast_data(sock, message):
for socket in CONNECTION_LIST:
if socket not in (server_socket, sock, listen):
try :
socket.send(message)
except socket.error as exc:
print '!!! An error occurred while writing to client. !!!'
print exc
socket.close()
CONNECTION_LIST.remove(socket)
Now no attempt will be made to send messages to the UDP listen socket, and the socket won't be closed due to error.
P.S. the code in your main loop that closes and reopens the listen socket is not necessary.

Can read from, but not assign to list - UnboundLocalError: local variable 'CLIENT_LIST' referenced before assignment

server.py
import socket, select, signal, errno, os, sys
from threading import Thread
def broadcast_data (sock, message):
for socket in CONNECTION_LIST:
#use:
#if socket != s and socket != sock:
#if you want to broadcast to all but sending client and server
# print sock.getpeername()[1]
# print CLIENT_LIST[0][1]
if socket != s:
try :
socket.send(message)
except :
#broken socket connection may be, chat client pressed ctrl+c for example
socket.close()
CONNECTION_LIST.remove(socket)
def handle_cli():
#process client data
data = sock.recv(RECV_BUFFER)
if data == '?':
broadcast_data(sock, hand_cl(CLIENT_LIST))
elif data == 'removeme':
hold = rem_cl(sock.getpeername()[1],CLIENT_LIST)
CLIENT_LIST = hold
elif data:
broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)
else:
sock.close()
CONNECTION_LIST.remove(sock)
def hand_inter(signum, frame):
print hand_cl(CLIENT_LIST)
def hand_qui(signum, frame):
sys.exit(0)
def hand_cl(cl):
totstring = "Current clients connected:"
for c in cl:
totstring+= " "+str(c)
totstring+="\n"
return totstring
def rem_cl(peername,cl):
cl = [(ip, port) for ip, port in cl if port != peername]
return cl
if __name__ == "__main__":
#keep track of socket descriptors
signal.signal(signal.SIGINT, hand_inter)
signal.signal(signal.SIGQUIT, hand_qui)
CONNECTION_LIST = []
CLIENT_LIST = []
RECV_BUFFER = 4096
HOST = 'localhost'
PORT = int(sys.argv[1])
ADDR = (HOST, PORT)
if len(sys.argv) > 2:
MAXHOST = int(sys.argv[2])
else:
MAXHOST = 2
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(ADDR)
s.listen(MAXHOST)
#add server socket to the list of readable connections
CONNECTION_LIST.append(s)
print "Chat server started on port " + str(PORT)
while 1:
#fix for signal interrupt
while True:
try:
read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])
except select.error, v:
if v[0] != errno.EINTR: raise
else: break
for sock in read_sockets:
#if sock in read socks == new s, new connection
if sock == s:
sockfd, addr = s.accept()
conmax = [MAXHOST, len(CONNECTION_LIST)]
conmax = str(conmax)
#send maxhost number for comparison
sockfd.send(conmax)
CONNECTION_LIST.append(sockfd)
CLIENT_LIST.append(addr)
print "Client (%s, %s) connected" % addr
broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)
#else it's a message from a pre-connected client
else:
Thread(target=handle_cli).start()
s.close()
Objective: Whenever a client sends message 'removeme', I want CLIENT_LIST to remove the specific client from the CLIENT_LIST. The method rem_cl works correctly and returns the correct results, however when I try to reassign CLIENT_LIST to the new updated list, I get the error:
line 26, in handle_cli
hold = rem_cl(sock.getpeername()[1],CLIENT_LIST)
UnboundLocalError: local variable 'CLIENT_LIST' referenced before assignment
However if I remove CLIENT_LIST = hold I get no error.
How do I reassign CLIENT_LIST to be this newly returned list?
In your function handle_cli(), when you attempt to assign a value to CLIENT_LIST, you are assigning it to a local variable (not a global variable). In order to assign that value to the global variable CLIENT_LIST that you are using in your script, you must first use the global statement:
global CLIENT_LIST
CLIENT_LIST = ...
The Python docs say, It would be impossible to assign to a global variable without global.
Also, check out the answer to the question use of "global" keyword in python.

Cannot Get Strings Across in a Socket Program

I have been looking at some code for a small chat program that I found online. It was originally written for 2.7, but it seems to work with 3.2. The only problem is that I cannot send strings, only numbers:
The chat.py file source code:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(input("Reply: "))
conn.send(reply)
conn.close()
And the client.py source file:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()
When I run these using two separate terminals, they work, but do not send strings.
Thank you
When you work with sockets, the message you're passing around should probably be in bytes, b'bytes'. In Python 2.x, a str is actually what a bytes is in Python 3.x
So your message should be something like:
message = b'Message I want to pass'
Check here http://docs.python.org/3.3/library/stdtypes.html for more information.
According to http://docs.python.org/3/library/functions.html#input input returns a str, which means you'll have to encode message into bytes as such:
message = message.encode()
Do verify that this is the right approach to convert str to bytes by checking the type of message.
Your socket code is correct, it was just failing due to an unrelated error due to raw_input vs input. You probably intended to read a string from the shell instead of reading a string and trying to evaluate it as Python code which is what input does.
Try this instead:
chat.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(raw_input("Reply: "))
conn.send(reply)
conn.close()
client.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(raw_input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()

Non-Blocking error when adding timeout to python server

I am writing a simple TCP server in python, and am trying to input a timeout. My current code:
import socket
def connect():
HOST = '' # Symbolic name meaning the local host
PORT = 5007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
s.settimeout(5)
conn, addr = s.accept()
print 'Connected by', addr
return conn
conn = connect()
while 1:
data = conn.recv(1024)
if not data: break
print data
conn.close()
Issue is when I try to connect I get an error at data = conn.recv(1024)
error: [Errno 10035] A non-blocking socket operation could not be completed immediately
Code works without the timeout.
You can turn on blocking:
# ...
conn.setblocking(1)
return conn
# ...
Try to set the timeout on the socket and the blocking on the connection. Like this:
import socket
def connect():
HOST = '' # Symbolic name meaning the local host
PORT = 5007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.bind((HOST, PORT))
s.listen(1)
return s
s = connect()
while 1:
conn, addr = s.accept()
print 'Connected by', addr
conn.setblocking(1)
data = conn.recv(1024)
conn.close()
if not data: break
print data
s.close()
Ran into the same problem 30 minutes ago. Found a simple non-elegant work around...if you give the socket time to breathe by doing time.sleep(1), catching the 10035 error and doing a retry it works. I'm using 2.7.5...maybe this is a bug that got fixed. Not real sure.
Code sample...please understand this is very simplistic test code I use (only recv 1 byte at a time).So where 's' is the socket with a 10s timeout and 'numbytes' is number of bytes I need...
def getbytes(s,numbytes):
din = ''
socketerror10035count = 0
while True:
try:
r = s.recv(1).encode('hex')
din += r
if len(din)/2 == numbytes:
print 'returning',len(din)/2, 'bytes'
break
except socket.timeout as e:
din = 'socket timeout'
break
except socket.error as e:
if e[0] == 10035 and socketerror10035count < 5:
socketerror10035count = socketerror10035count +1
time.sleep(1)
else:
din = 'socket error'
break
except:
din = 'deaddead'
break
return din
For Python 3 and above, the above code which references e as a scriptable object will need to be changed to "e.errno". And, of course the print statements require parenthesis around the arguments.
Additionally, you may want to change the "except socket.error as e:" line to "except BlockingIOError as e:". However, the code works as is under Python 3.8.5 on Windows.

the code stuck after receiving one byte from the socket ?

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.

Categories

Resources