How to Set Up an Always-On Server for Python Scripts Using PythonAnywhere
by FuzzyPotato in Circuits > Computers
56 Views, 0 Favorites, 0 Comments
How to Set Up an Always-On Server for Python Scripts Using PythonAnywhere
This guide will walk you through the steps of hosting and running your Python script on the PythonAnywhere platform.
Why might we want to do this you ask?
Great question!
Once we have a computer that is always on and connected to the internet we can create all sorts of wonderful and creative projects! We will use this Instructable as the first in a series of Instructables that will range from an automated daily chatGPT trivial email to a fullstack flask website and even your very own custom made API!
We will start of with a basic script that logs the date and time and save it to a text file. This will allows us to get familiar with pythonanywhere and see it work in real time!
Supplies
What you will need:
- A PC
- Some excitement
- About 30 mins
Navigate to PythonAnywhere
Go to PythonAnywhere.
Click Pricing & Sign up
Sign Up to the Free Account
If you’re new, choose the free Beginner Account to get started (you can upgrade later if needed).
Input a Username and Password
Input a username, your email address and a password and then select register.
Confirm You Email Address
You'll be redirected to a page asking you to Confirm your email address.
Head to the email account that you signed up with and open the email.
Use the Link
Click on the link to confirm your account.
You'll be accepted and redirected to the main dashboard.
The Introductory Tutorial
Then you'll see an introductory tutorial. I recommend you click through it. Its worth the minute that you'll spend on it.
Explore the Dashboard
Once that's out of the way you'll be on the main dashboard page.
Click on the Files tab.
Create a Python Script
Then we can create a new python file!
Input a name for the file and then select new file. This will create our new python file.
Upload the Script
Then copy and paste this code into the new file:
Then select save and then run.
Test the Script in the Console
You should see the system start up in the console and eventually after about 5 seconds you will see your script write task executed at: xx/xx/xxxx !!!
Navigate to the Script Log
Now if you navigate back to your directory you will see the new file that your script just created!
It will be called task_log.txt
Click on it to open it up.
View the Script Log
Then BAM!
You will see the same text that wrote to the console saved to your new text file log.
Well done! you have create a script on pythonanywhere a always on cloud server!
Schedule the Script (Optional)
First we will open the script back up and copy the script path.
Navigate to the Tasks Tab
Then navigate to the Tasks tab and click on it.
Create a Task
The set a time that is a couple of minutes in ahead of the server time. For me the time was 10:50 so I put 10:52.
Then paste in the file path that we copied earlier.
And then select Create.
Let It Run on Schedule
Then you'll see that the task has been scheduled.
Wait a couple of minutes until your schedule time has passed and then navigate back to the files page.
Check the Log File
You'll notice that your task_log.txt file will show that it was modified at the same time that you schedule your task for. Remember that I scheduled mine for 10:52? Right one time!
Check Out Your Handy Work!
Open it up and your will see that the script ran on schedule and logged it to the task_log.txt file!
Congratulations!
You have learn to set up an always on server on pythonanywhere. You created a logging script and scheduled it.
Now that you can use it to create many other projects like
- A Python website
- or build your very own API
- Maybe a daily trivia email
- And much more!
If you have a look at my page you'll find Instructables for these too.
If you found this helped or was valuable in anyway please drop a like! It helps more than you know!