Python client-server using socket.io
What is Socket.io?
Socket.IO is a transport protocol that enables real-time bidirectional event-based communication between clients (typically, though not always, web browsers) and?server.
When a server wants to communicate with a client it?emits?an event.?The client registers event handler functions with the?socketio.Client.event()?or?socketio.Client.on().
Note: It is recommended to create a virtual environment in your working directory for easy installation of required libraries. For details visit?venv — Creation of virtual environments — Python 3.9.7 documentation
Required packages:
pip install python-socketio
pip install "python-socketio[client]"
pip install eventlet
Server side:
To use socket.io some required libraries are:
socketio
eventlet : This library is used for network-related work. We can use it to run a webserver.
1.?????Creating a server instance.
sio?=?socketio.Server()
2.????We will use Eventlet server.
app?=?socketio.WSGIApp(sio,?static_files={
????'/':?{'content_type':?'text/html',?'filename':?'index.html'}
})
The?wsgi?module provides a simple and easy way to start an event-driven WSGI server. This can serve as an embedded web server in an application.
Eventlet.listen() is a convenience function for opening server sockets.
eventlet.wsgi.server(eventlet.listen(('',?5000)),?app)
3.????On client connection.
@sio.event
def?connect(sid,?environ):
print('server?connected?to?client?with?session?ID?=??',sid)
4.????Upon connection, the server assigns the client a unique session identifier. The application can find this identifier in the?sid?attribute.
print('my sid is', sio.sid)
5.????For sending data to client.
@sio.event
def?my_message(sid,data):
????sio.send(data)
????print('data?sent?from?server?')
6.????For receiving data from client.
领英推荐
@sio.event
def?response(sid,recdata):
????print(recdata)
7.?????Write a disconnect?handler which invokes if application initiated disconnects, server initiated disconnects, or accidental disconnects, for example due to networking failures.
@sio.event
def?disconnect(sid):
????print('disconnect=>SERVER?',?'SID:?',sid)
?
Client side:
1)????Create a client instance.
sio?=?socketio.Client()
2)???Connect to server.
@sio.event
def?connect():
????print('connection?established')
3)???For receiving data from server.
@sio.event
def?message(data):
????print(data)
4)???For sending data to server.
@sio.event
def?send_message():
????sio.emit(event?=?'response',?data?=?{'message':?'HI?from?client'})
????print('data?sent?to?server')
5)???The connection to a server is established by calling the?connect()?method:
sio.connect('https://localhost:5000')
6)???At any time the client can request to be disconnected from the server by invoking the?disconnect()?method:
@sio.event
def?disconnect():
????print('disconnected?from?server=>CLIENT')
7)????If the application does not have anything to do in the main thread and just wants to wait until the connection with the server ends, it can call the?wait()?method:
sio.wait()
Unity | C# | Python | Node.js | Backend | Interactive Experience | GenAI
3 年Keep working hard, keep rolling.