Nobody likes doing tedious and repetitive tasks. I received an email notifying me that I had to submit a daily ‘health declaration check’ as a form on a website before playing my favorite slots on https://gamblersarea.com/. Since I lead a boring and routine lifestyle, I thought I would automate this task. It would be a good excuse to refresh my knowledge on Python’s Requests module as well as get more hands on experience with cron jobs.
HTTP Requests are sent out by hosts (your browser or computer) for specific web resources, which are then answered by HTTP Responses that provide the web resource requested.
Requests come in multiple flavors but the most common requests are GET requests and POST requests. GET requests are HTTP requests usually sent to ‘get’ or retrieve information from the web while POST requests are being used to send or ‘post’ data to the internet (this could be adding a blog post, making a social media comment, or in our case, submitting a web form)
POST Requests are separated into 2 sections called the Head, which contain Headers, and the Body. For this particular request, the Header takes up the first 15 lines of the request and the body of our request begins on line 17.
We can see where each of the fields in the form corresponds to the fields in line 17 of the request. We can thus write some code to modify the contents submitted in line 17 depending on the day of the week and where I will be on that day.
Now that we have some idea of what a valid HTTP request and corresponding response looks like, we can use Python and its request module to craft and send our own custom HTTP POST request to help submit this form for us automatically.
I map the headers to a Python dictionary and also use another dictionary to record my places of interest.
I perform some logic to ensure that the date is submitted in a format that the form expects it to be.
Finally, I add these variables into my data string that will form the body of our POST request before finally sending out the request.
Now that we have our simple Python script, we now have to schedule a cronjob to automatically run the script every day.
Cron jobs are a UNIX time-based job scheduler where Users can set up and schedule jobs to run periodically at fixed times, dates, or intervals.
In order for a scheduled cronjob to be run at the set time, the computer needs to be running and connected to the internet during the scheduled job for it to be executed. Instead of running the cronjob from my laptop and having to ensure it is on and connected at the scheduled time of execution, I am going to use the deth* (pronounced deathstar), my raspberry pi 4 server which I have hooked into the back of a tv in my room. It’s a kali machine which I use for lightweight, in-depth, thorough scans were leaving my laptop running for 8hours is no option.
I use Dataplicity, which offers a VPN-like service to connect remotely into your Raspberry Pi without having to set up a port forward or deal with any dynamic DNS services. Using Dataplicity, I can remotely access deth* wherever I have internet.
Cronjobs can be scheduled on a per-user basis via something called crontab. To access the user-specific crontab, we do so via
The syntax of a cronjob command is 5 optional parameters followed by the command you want to run at the specified time.
I’m running my cronjob at the start of the 50th minute of the 2nd hour of the day, or at 02 50 UTC time, which is about 9.50am local time.
I also redirect output from the python file to logs so that I can check periodically to make sure the cronjob is running as expected.
With this done, I can rest easy knowing that my forms are being submitted in an automated non-intervention-requiring manner!
Follow me on Twitter: @vis1na