+ 2
Is there a way of networking in python other than socket?
Suppose I have a socket connection named con. When I use con.recv(1024), the program gets stuck in a loop until it receives something. Is there a way to avoid this? Moreover, receiving through socket is limited by buffer size. So, is there some other way of networking?
7 Respostas
+ 2
You can set a connexion to nonblocking : conn.setblocking(False).
E.g :
conn.setblocking(False)
try:
conn.recv(1024)
except:
pass
It is in a try / except statment because if there is nothing to receive, then an error occurs.
+ 2
Thanks for the answers
+ 2
How can I do this?
+ 1
Concerning the buffer limitations, I find it great. You can send first the length of the data you are going to send, then the datas, so the receiver knows perfectly how much data to receive.
def send(conn, datas) :
conn.send(str(len(datas)).encode())
conn.sendall(datas.encode())
def receive(conn) :
amount = int(conn.recv(1024).decode())
return conn.recv(amount)
+ 1
You're welcome!
0
From what I know, python sockets are based on c sockets, that are themselves using your OS APIs.