【发布时间】:2021-07-12 19:48:35
【问题描述】:
所以我试图从压力传感器 Boditrak 获取数据。它是通过 USB 连接的,但我不确定它使用的是哪个端口......当我连接它时,我有这个数据端口 this is how I see it。它有自己的软件,但我需要通过 Python 来获取它。 这是我写的代码:
import socket
serverAddress = 'http://localhost/api'
serverPort = 63342
bufferSize = 4096
def connect(self):
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(10)
print("Connecting to server")
s.connect((serverAddress, serverPort))
print("Connected to server\n")
response = s.recv(bufferSize)
print(response.decode("utf-8"))
现在我不确定我在做正确的事......但我不确定我应该如何得到它。任何帮助表示赞赏。 而且数据看起来像这样accessed in google chrome,我每秒都会得到一个新帧
当我在终端中运行脚本时,我什么也得不到。 当我阅读手册时,它说:“DataPort 设备使用 REST API 通过 wifi 网络与客户端设备(PC、平板电脑、手机)通信。DataPort 设备的主要作用是扫描一个或多个 Boditrak 传感器垫规定的频率并将这些读数存储在缓冲区中”
我还需要有服务器端和客户端吗?如果是,它应该是什么样子?
谢谢!
PS。这是我想要获取的实时数据 也许我应该将此称为 GET /api/sse HTTP/1.1。但是怎么做呢?
例如,我采取了另一种方法,但仍然没有答案...
import socket
from urllib import parse
def connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
url = parse.urlparse('http://localhost/api')
s.connect((url[1], 80))
msg = 'GET' + 'http://localhost/api' + 'HTTP/1.1\r\n\r\n'
s.send(msg.encode('utf-8'))
response = s.recv(4096)
data = response.decode('utf-8')
print(data)
if __name__ == "__main__":
connect()
我得到以下答案:
b''
更新:我现在得到一些数据。代码如下:
import socket #for sockets
import sys #for exit
def connect():
#create an INET, STREAMing socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Failed to create socket')
sys.exit()
print('Socket Created')
host = '127.0.0.1';
port = 80;
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#could not resolve
print('Hostname could not be resolved. Exiting')
sys.exit()
#Connect to remote server
s.connect((remote_ip , port))
print('Socket Connected to ' + host + ' on ip ' + remote_ip)
#Send some data to remote server
message2 = b"GET /api/sse HTTP/1.1\r\n\r\n"
try:
# Set the whole string
s.sendall(message2)
except socket.error:
# Send failed
print('Send failed')
sys.exit()
print('Message send successfully')
# Now receive data
reply2 = s.recv(16384)
print('Frames:', reply2.decode())
if __name__ == "__main__":
connect()
然后回复:
Socket Created
Socket Connected to 127.0.0.1 on ip 127.0.0.1
Message send successfully
Frames: Access-control-allow-origin: *
Access-control-allow-methods: GET, OPTIONS
Content-type: application/json
Cache-control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0
Content-Length: 2583
{
"device":{ "class":"Boditrak DataPort", "name":"DataPort-******", "id":"*********", "address":"127.0.0.1", "model":"wia" },
"sensors":[ { "name":"**********", "columns":32, "rows":32, "width":470, "height":470, "minimum":0, "maximum":200, "units":"mmHg" } ],
"frames":[ { "id":719, "time":"2021-04-19 16:19:47.041", "readings":[ [ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
,1,2,1,1,2,1,1,1,0,0,0,0,1,1,0,2,2,2,2,1,1,2,3,0,0,0,1,1,2,0,1 ] ] } ],
"filters":{ "spot":false, "smooth":false, "noise":false },
"time":"2021-04-19 16:19:47.097",
"frequency":27000,
"yield":false,
"calibrated":true,
"sensorsRequired":0,
"others":[ ]
}
我必须弄清楚如何连续获得它。仍然看到最后一帧...
最后一次更新!
import socket #for sockets
import sys #for exit
import json
import time
t_end = time.time() + 60 * 0.2
def connect():
#create an INET, STREAMing socket
try:
global s
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Failed to create socket')
sys.exit()
print('Socket Created')
host = '127.0.0.1';
port = 80;
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#could not resolve
print('Hostname could not be resolved. Exiting')
sys.exit()
#Connect to remote server
s.connect((remote_ip , port))
print('Socket Connected to ' + host + ' on ip ' + remote_ip)
message2 = b"GET /api/sse HTTP/1.1\r\n\r\n"
try:
# Set the whole string
s.sendall(message2)
except socket.error:
# Send failed
print('Send failed')
sys.exit()
print('Message send successfully')
while time.time() < t_end:
reply2 = s.recv(4096).decode('utf-8')
response = json.dumps(reply2)
print(response)
if __name__ == "__main__":
connect()
此代码正在运行,它可以实时为我提供数据。我遇到了一个错误问题:[WinError 10053] 已建立的连接被您主机中的软件中止 - 在我停用我的防病毒软件后它可以工作。
标题
【问题讨论】:
-
你在 Chrome 中输入哪个地址?
-
localhost/api/sse 在这里我得到了所有的实时数据。 (见帖子中实时数据截图)
-
如果没有被告知,Chrome 肯定不会使用端口 63342,是吗?为什么在 Python 代码中使用那个端口?
-
我选择了那个,因为当我查看 CMD 时,它是唯一一个监听该 IP 地址的。你说的对。我用下面的代码修改了最初的帖子。
-
我现在正在获取数据。检查更新。现在我只需要不断地做到这一点