How to Build a Python Socket.IO Test Script for API Testing
This guide shows how to create a Python 3.7 Socket.IO client script for testing socket interfaces, including installation of the python‑socketio package, handling authentication tokens with tools like Postman, and a complete example script with repository links for reference.
When a team started learning Python, the author compiled a Python version of a Socket.IO testing script to replace existing Java and Groovy implementations used for socket interface testing.
The script requires Python 3.7 and the python-socketio package, which can be installed via: pip3 install python-socketio Authentication tokens are not generated by the script; users should obtain them after logging in using tools such as Postman, Charles, or Fiddler.
The full script is provided below. It creates a socketio.Client, connects to the target URL, registers the user, joins a room, and prints received messages.
import socketio
import time
def func(token="", uid=0, room=0):
sio = socketio.Client()
event = 'my_event'
@sio.event()
def my_response(data):
# handle the message
print(data)
@sio.event
def connect():
print("I'm connected!")
@sio.event
def connect_error():
print("The connection failed!")
@sio.event
def disconnect():
print("I'm disconnected!")
url = 'http://ailearn-instruction-stress.xk12.cn:38999/?systemId={uid}&loginType=3&token={token}&userType=1'
url = url.format(uid=uid, token=token)
sio.connect(url, transports=["websocket"])
print('my sid is', sio.sid)
time.sleep(3)
sio.emit(event, {"cmd": "register", "userId": uid, "role": "T", "deviceVersion": "1.0", "s_sid": sio.sid, "token": token})
sio.emit(event, {"cmd": "joinRoom", "roomId": room})
time.sleep(3)
if __name__ == '__main__':
func(token="519594cc8e3a4bd68679b2b613c20536", uid=61951375269, room=43548)The script has been tested successfully and is intended as a reference implementation.
Source code and related resources are available at the following repositories:
Gitee: https://gitee.com/fanapi/tester
GitHub: https://github.com/JunManYuanLong/FunTester
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
