@bennr01 said:
@Brun0oO If you only need websockets, you could also use Autobahn|Python, which runs on asyncio and/or Twisted.
That's the solution, thank you !!
In order to install autobahn, i did under Stash "pip install zope.interface".
As "pip install twisted" failed, i downloaded the twisted code source and moved manually the twisted package to the site-packages folder. Then, i performed a "pip install autobahn".
Here the code for the websocket server websocket-server.py:
from autobahn.asyncio.websocket import WebSocketServerProtocol, \
WebSocketServerFactory
import socket
def get_local_ip_addr(): # Get the local ip address of the device
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Make a socket object
s.connect(('8.8.8.8', 80)) # Connect to google
ip = s.getsockname()[0] # Get our IP address from the socket
s.close() # Close the socket
return ip # And return the IP address
class MyServerProtocol(WebSocketServerProtocol):
def onConnect(self, request):
print("Client connecting: {0}".format(request.peer))
def onOpen(self):
print("WebSocket connection open.")
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
# echo back message verbatim
self.sendMessage(payload, isBinary)
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
if __name__ == '__main__':
try:
import asyncio
except ImportError:
# Trollius >= 0.3 was renamed
import trollius as asyncio
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000")
factory.protocol = MyServerProtocol
loop = asyncio.get_event_loop()
coro = loop.create_server(factory, '0.0.0.0', 9000)
server = loop.run_until_complete(coro)
print("Server started at {0}:{sock[1]}".format(get_local_ip_addr(),sock=server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
server.close()
loop.close()
The code for the websocket client is the same as the previous one.
Brun0oO