How to implement a non-blocking socket server in python

A similar but different question:

I have an IRC client that generates strings. This IRC client uses the hook to call the method (somone_said) when someone says something. I want to send this line through a socket to my flash client.

I have a working client in flash and a server in python, but the problem is that it blocks: 1) while listening to the client connection 2), waiting for the following message to be generated

This stops the IRC client from accessing other inputs.

It seems to me that I need to create my socket in a separate thread, but this creates three more problems. 1) how my method controlled by some_said events gets access to the socket 2) What should I do if someone says something when the client connection is absent (while listening) or if the client closed the connection. 3) How to check if a thread is alive, and if you don’t open a new one?

My lock server code:

# Echo server program import socket import sys HOST = None # Symbolic name meaning all available interfaces PORT = 7001 # Arbitrary non-privileged port s = None def startListening(): print "starting to listen" for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, socket.AI_PASSIVE): af, socktype, proto, canonname, sa = res try: s = socket.socket(af, socktype, proto) except socket.error as msg: s = None continue try: s.bind(sa) s.listen(1) except socket.error as msg: s.close() s = None continue break if s is None: print 'could not open socket' sys.exit(1) conn, addr = s.accept() print 'Connected by', addr while 1: try: data = conn.recv(1024) except: print "cannot recieve data" break if not data: break print data message = "" while not "quit" in message: message = raw_input('Say Something : ') # This will come from event driven method try: conn.sendall(message) except Exception as exc: print "message could not be sent" break conn.close() print "connection closed" while 1: startListening() 

The xchat python script module works like this (requires HexChat to start)

 __module_name__ = "Forward Module" __module_version__ = "1.0.0" __module_description__ = "Forward To Flash Module by Xcom" import sys import xchat def someone_said(word, word_eol, userdata): # method called whenever someone speaks in IRC channel username = str(word[0]) # From IRC contains the username string message = str(word[1]) # From IRC contains the user message sendString = username + " : " + message send_to_server(sendString) def send_to_server(message): # send over socket method to be implemented here xchat.hook_print('Channel Message' , someone_said) 

I’ve been banging my head on the wall for several days now. Help me obi wan kenobi, you are my only hope.

+4
source share
1 answer

Take a look at Asyncore, it is definitely made for what you are looking for :)

http://docs.python.org/2/library/asyncore.html

Greetings

TO.

+2
source

Source: https://habr.com/ru/post/1484410/


All Articles