

Python调用socket模块获取路由器接口数据
source link: https://chegva.com/5955.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Python调用socket模块获取路由器接口数据
配合开发测试物联网多路网卡,需要访问路由器接口获取信号量等网络状态信息,然后写入到日志文件上传到云端,由于路由器接口是tcp格式,需要使用socket访问。
import socket
import json
import time
import datetime
host = '192.168.1.1'
port = 8888
# 构建要发送的参数
reqid = int(time.time() * 1000)
message = { "action": "get_wwans_status", "reqid": str(reqid) }
def send_tcp_request(host, port, message):
# 创建一个TCP套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# 连接到指定的主机和端口
sock.connect((host, port))
# 发送数据
message = json.dumps(message)
sock.sendall(message.encode('utf-8'))
# 接收响应数据
response = sock.recv(1024)
# 处理响应数据
# print('Response:', response.decode('utf-8'))
json_data = json.loads(response.decode('utf-8'))
return json_data.get("data", [])
finally:
# 关闭套接字连接
sock.close()
while True:
try:
# 发送TCP请求
data_array = send_tcp_request(host, port, message)
current_date = datetime.datetime.now().strftime('%Y%m%d')
file_name = '/data/log/filebeat_upload/router_' + current_date +'.log'
with open(file_name, 'a') as output_file:
for data_object in data_array:
timestamp = datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S.%f')[:-3] + 'Z'
data_object["@timestamp"] = timestamp
data_object["ttl"] = 0
data_object["log_type"] = 'chegva_router_monitor'
output_file.write(json.dumps(data_object) + '\n')
except Exception as e:
continue
# 等待1秒
time.sleep(1)
Recommend
-
39
Socket接口的分层 Socket的英文原本意思是 孔 或 插座 。但在计算机科学中通常被称作为 套接字 ,主要用于相同机器的不同进程间或者不同机器间的通信。Socket的使用很多网络编程的...
-
10
【原文链接: https://cloud.tencent.com/developer/article/1392328】 IronPython是一种在.NET上实现的Python语言,使用IronPython就可以在.NET环境中调用Python代码。
-
8
在python中我们可以使用requests模块来实现调用远程接口 一、安装requests模块 pip install requests 二、使...
-
4
利用 Xpocket 的 ss 插件获取 socket 信息文章>利用 Xpocket 的 ss 插件获取 socket 信息利用 Xpocket 的 ss 插件获取 socket 信息
-
11
3.napi代码实现部分 ①.代码部分 路径:\OpenHarmony\foundation\ace\napi\sample\native_module_socket_server\native_module_socket_server.cpp 注意:native_module_socket_server文件夹自行创建 /* * Copyright...
-
5
Python 巧妙地将rpc接口封装成pythonic的链式调用2016.10.25这是一个外国人实现的Zabbix(一个开源监控工具)的Python Client——pyzabbix里的代码片段。RPC调用Rpc调用的...
-
2
OpenHarmony之 网络管理 Socket 模块的使用 原创 精华 OpenHarmony之 网络管理 Socket 模块 Socket 模块可以用来进行数据传输,支持TCP和UDP两种协议。...
-
6
OpenHarmony之 网络管理 Socket 模块的使用-51CTO.COM OpenHarmony之 网络管理 Socket 模块的使用 作者:Buty9147 2022-05-26 15:28:03 本期将为您展示一下:如何 使用 Socket模块实现...
-
9
Python 封装SNMP调用接口 PySNMP 是一个纯粹用Python实...
-
2
Python 调用Zoomeye搜索接口 精选 原创 lyshark 2023-01-05 09:50:14...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK