-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalScriptnew.py
More file actions
62 lines (50 loc) · 1.59 KB
/
Copy pathlocalScriptnew.py
File metadata and controls
62 lines (50 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Import the required libraries:
import requests
from requests.auth import HTTPBasicAuth
import json
# Define the API url and endpoint:
# url = 'http://localhost:8000/milldata'
# endpoint = '/devices/{device_id}/timestamps/'
# url = 'http://localhost:8000/milldata/devices/1/timestamps/'
url = 'http://localhost:8000/milldata/devices/1/'
# Define the credentials:
username = 'testuser1'
password = 'Test4work'
# company_id = '2'
# device_id = '1'
# Define the MillData object to be sent to the API:
milldata = {
'katta_time': '2022-03-20 04:21:00',
'katta_weight': 100.0,
'circle': 18,
'feed_time': 9,
'circle_hold': 15,
'actual_hold': 900,
'feed_status': True,
'overload_status': False
}
print(f"Data is {milldata} and data type is {type(milldata)}")
# Convert the data to JSON format
json_data = json.dumps(milldata)
print(f"JSON data is {json_data} and data type is {type(json)}")
# Set the headers for the POST request
headers = {
'Content-Type': 'application/json'
}
# Send the POST request to the API url with the MillData object and credentials:
response = requests.post(
# url + endpoint.format(device_id=device_id),
url,
# json=milldata,
data=json_data,
headers=headers,
# auth=HTTPBasicAuth(username, password),
auth=(username, password),
# params={'company_id': company_id}
)
# Check the response status code and print the response message:
if response.status_code == 201:
print('MillData sent successfully.')
else:
print('Failed to send MillData:', response.text)
# Putting it all together, the code block would look like this: