Home > Mobile >  I've got this unix script and I'm trying to translate it to python. Problem is I don'
I've got this unix script and I'm trying to translate it to python. Problem is I don'

Time:10-18

1 17-23 * * * curl -X PUT -d EM_OperatingMode=10 --header 'Auth-Token: 512532eb-0d57-4a59-8da0-e1e136945ee8' http://192.168.1.70:80/api/v2/configurations

This is the code, again, the problem is I have no idea what any of it means.

CodePudding user response:

It is crontab, example:

*    *    *   *    *  Command_to_execute
|    |    |    |   |       
|    |    |    |    Day of the Week ( 0 - 6 ) ( Sunday = 0 )
|    |    |    |
|    |    |    Month ( 1 - 12 )
|    |    |
|    |    Day of Month ( 1 - 31 )
|    |
|    Hour ( 0 - 23 )
|

Min ( 0 - 59 )

Here is the source: https://www.tutorialspoint.com/unix_commands/crontab.htm

And second part is curl with some options (flags): https://curl.se/docs/manpage.html

CodePudding user response:

It seems to be cron job that runs given command automatically on given interval "At minute 1 past every hour from 17 through 23." .

Similar python code would be

import requests

requests.put(
  "http://192.168.1.70:80/api/v2/configurations", 
  data={"EM_OperatingMode":10}, 
  headers={"Auth-Token":"512532eb-0d57-4a59-8da0-e1e136945ee8"}
 )

CodePudding user response:

The code you took is in crontab of Unix system. Let's split the code into two parts.

The first part - '1 17-23 * * * ' represents a schedule. It means between 5PM to 11PM, the program runs every hour at the first minute of that hour. Crontab follows min hour day month weekday. (Refer: https://crontab.guru/#1_17-23_*_*_*)

The second part curl -X PUT -d EM_OperatingMode=10 --header 'Auth-Token: 512532eb-0d57-4a59-8da0-e1e136945ee8' http://192.168.1.70:80/api/v2/configurations represents a PUT request to an URL.

curl is a program used to communicate with a server. You use a browser to interact with a server right. You can think of curl as a command line tool to do the same. The parameter contains PUT request which updates data in the server. Here EM_OperationMode = 10 is being passed to the server for updating. --header represents parameters to be sent to the URL. Here an authentication token is being sent to the URL to validate its access. Finally, the URL for communication is http://192.168.1.70:80/api/v2/configurations.

So, the value is being passed to the server at 192.168.1.70 as per the schedule I mentioned above. You can use Python requests library to replicate the server communication while you can retain the scheduler for calling the python program to run it as per the same schedule.

  • Related