Converting python code to matlab code

Can someone please help me to convert the below python code into MATLAB?
%python code
import requests
import random
import time
ENDPOINT = "api.somewebsite.com"
LABEL1 = "label1"
LABEL2 = "label2"
TOKEN = "XXXXXXXXXX"
DELAY = 1 # Delay in seconds
def post_var(payload, url=ENDPOINT, device=DEVICE_LABEL, token=TOKEN):
try:
url = "http://{}/api/v1.1/dev/{}".format(url, LABEL1)
headers = {"X-Auth-Token": token, "Content-Type": "application/json"}
attempts = 0
status_code = 400
while status_code >= 400 and attempts < 5:
print("[INFO] Sending data, attempt number: {}".format(attempts))
req = requests.post(url=url, headers=headers,
json=payload)
status_code = req.status_code
attempts += 1
time.sleep(1)
print("[INFO] Results:")
print(req.text)
except Exception as e:
print("[ERROR] Error posting, details: {}".format(e))
def main():
value = random.random() * 100
payload = {LABEL2: value}
post_var(payload)
if __name__ == "__main__":
while True:
main()
time.sleep(DELAY)
Thanks,
A confused student

3 件のコメント

KSSV
KSSV 2020 年 6 月 18 日
If you tell what for the code is, you may already have a function for that in MATLAB.
abc xyz
abc xyz 2020 年 6 月 18 日
The code is for sending data to a website via http.
KSSV
KSSV 2020 年 6 月 18 日
Have a look on publish. I am not sure whether this addresses your question or not.

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 6 月 18 日
編集済み: Ameer Hamza 2020 年 6 月 18 日

1 投票

8 件のコメント

abc xyz
abc xyz 2020 年 6 月 18 日
I tried webwrite but couln't do the headers part. I am a begginer in matlab. Please help...
Ameer Hamza
Ameer Hamza 2020 年 6 月 18 日
abc xyz
abc xyz 2020 年 6 月 18 日
編集済み: abc xyz 2020 年 6 月 18 日
I tried the following code but the server returned a 401 error, even when the python code works fine.
url = 'https://api.somewebsite.com/api/v1.1/dev/label1/';
headerFields = {'X-Auth-Token', 'xxxxxxxxxxxxxxxxxxx'};
options = weboptions('HeaderFields', headerFields, 'ContentType','json');
response = webwrite(url,['label2',50]);
please see what is the mistake.
Ameer Hamza
Ameer Hamza 2020 年 6 月 18 日
You need to pass options to webwrite
response = webwrite(url,['label2',50],options);
abc xyz
abc xyz 2020 年 6 月 18 日
Now it is giving error 400 "Bad request". I checked the python code and the python code is still working. Please guide...
Ameer Hamza
Ameer Hamza 2020 年 6 月 18 日
Try following modifications one by one, and with combinations too, and see if it work.
Change the options to
headerFields = {'X-Auth-Token', 'xxxxxxxxxxxxxxxxxxx'; 'Content-Type', 'application/json'};
options = weboptions('HeaderFields', headerFields);
or
response = webwrite(url,['label2=',num2str(50)],options);
or
response = webwrite(url,'label2',50,options);
or
response = webwrite(url,'label2',num2str(50),options);
abc xyz
abc xyz 2020 年 6 月 18 日
Third one worked. Thanks a lot.
Ameer Hamza
Ameer Hamza 2020 年 6 月 18 日
I am glad to be of help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 6 月 18 日

コメント済み:

2020 年 6 月 18 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by