+ 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?

29th Oct 2019, 8:25 AM
Jalaj Kumar
Jalaj Kumar - avatar
7 odpowiedzi
+ 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.
29th Oct 2019, 8:43 AM
Théophile
Théophile - avatar
+ 2
Thanks for the answers
29th Oct 2019, 8:54 AM
Jalaj Kumar
Jalaj Kumar - avatar
+ 2
How can I do this?
30th Oct 2019, 6:43 AM
Jalaj Kumar
Jalaj Kumar - avatar
+ 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)
29th Oct 2019, 8:51 AM
Théophile
Théophile - avatar
+ 1
You're welcome!
29th Oct 2019, 8:55 AM
Théophile
Théophile - avatar
0
From what I know, python sockets are based on c sockets, that are themselves using your OS APIs.
30th Oct 2019, 8:11 AM
Théophile
Théophile - avatar