1

So i am trying to get the data from a pressure sensor Boditrak. It is connected via USB but I am not sure which port is it using... When I am connecting it I have this Data Port this is how I see it. It has its own software, but I need to get it through Python. This is the code that I wrote:

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"))

Now I am not sure I am doing the right thing... but I am not sure how am I supposed to get it. Any help is appreciated. Also the data looks like this accessed in google chrome and I get every second a new frame

When I am running the script in terminal I get nothing. When I am reading the manual it says: "The DataPort device communicates with client devices (PC, tablet, phones) over a wifi network using a REST API. The primary role of the DataPort device is to scan one or more Boditrak sensor mats at a prescribed frequency and store those readings in a buffer"

Also do I need to have a server side and a client side? If yes, how is it supposed to look like?

Thank you!

PS. This is the live data that I want to get image with continues data Maybe I should call this GET /api/sse HTTP/1.1. But how?

For example, i took another approach but still no answer...

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()

I get the following answer:

b''

UPDATE: I get some data now. Here is the code:

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()

And the reply:

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,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,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":[ ]
}

I have to figure it out how to get it continuously. Still sees the last frame...

Last update!

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()

This code is working and it is giving me data in real time. I encountered problems with an error: [WinError 10053] An established connection was aborted by the software in your host machine -- and after I deactivated my antivirus it works.

Heading

Gabrielaa5
  • 11
  • 2
  • Which address do you enter in Chrome? – Armali Apr 18 '21 at 15:33
  • http://localhost/api/sse here I get all the live data. (see the screenshot of live data in the post) – Gabrielaa5 Apr 18 '21 at 18:53
  • Surely Chrome doesn't use port 63342 if not told to, does it? Why do you use that port in the Python code? – Armali Apr 18 '21 at 19:35
  • Well then which one should I use? How do I check which Port all these data are stored to? – Gabrielaa5 Apr 19 '21 at 08:50
  • You didn't answer my question. However, if a HTTP request from Chrome works, the approach in your "answer" below (which should've rather been added to the question post) should also work; perhaps just the missing spaces between `GET`, `http://localhost/api` and `HTTP/1.1\r\n\r\n` prevent that. – Armali Apr 19 '21 at 11:10
  • 1
    I chose that one because when I looked on CMD it was the only one listening on that IP address. You are right. I modified the initial post with the code below. – Gabrielaa5 Apr 19 '21 at 14:05
  • Now, did you try with the missing spaces inserted? – Armali Apr 19 '21 at 15:28
  • Yes. But I get nothing. No error, just blank space.. – Gabrielaa5 Apr 19 '21 at 15:30
  • I'm puzzled. Could you `print(msg.encode())` in the script and show the output? – Armali Apr 19 '21 at 15:54
  • Yes. I am getting: b'GET http://localhost/api HTTP/1.1' But now I am getting this error when I am asking for the response = s.recv(4096): ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine – Gabrielaa5 Apr 19 '21 at 15:59
  • Hm - it should be `b'GET … HTTP/1.1\r\n\r\n'`; where are the CR/NL pairs gone? (It's rather a rhetorical question.) Without them, it can't work. – Armali Apr 19 '21 at 16:06
  • 1
    I am getting data now. Check the update. Now I just have to make it continuously – Gabrielaa5 Apr 19 '21 at 16:29
  • Congratulation! - Now, `s.recv()` is called just once; perhaps call it in a loop. – Armali Apr 19 '21 at 16:48

1 Answers1

0

To ensure your script runs in the terminal you'll need to tell python to execute your method. To do that you can use an import guard. Add the following to the bottom of your file. This'll ensure that your connect method executes.

if __name__ == "__main__":
    connect()

Given that you can access your data stream through the browser, you could make your life easier by using the requests library instead of working with sockets. It has the potential to save time by handling low level socket work for you.

For example:

import time
import requests

def connect():
    url = 'http://localhost/api'
    wait_time = 1
    while True:
        time.sleep(wait_time)  # wait for 1s
        r = requests.get(url)  # get data
        # print output to console
        ​print(r.text) # text based output
        print(r.json())  # json output <- probably what you need given the screenshot

if __name__ == "__main__":
    connect()
Hevlastka
  • 1,878
  • 2
  • 18
  • 29