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

Python cloud server.png

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:

  1. A PC
  2. Some excitement
  3. About 30 mins

Navigate to PythonAnywhere

PythonCapture0.PNG

Go to PythonAnywhere.

Click Pricing & Sign up

Sign Up to the Free Account

PythonCapture1.PNG

If you’re new, choose the free Beginner Account to get started (you can upgrade later if needed).

Input a Username and Password

PythonCapture2.PNG

Input a username, your email address and a password and then select register.

Confirm You Email Address

PythonCapture3.PNG

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

PythonCapture4.PNG

Click on the link to confirm your account.

You'll be accepted and redirected to the main dashboard.

The Introductory Tutorial

PythonCapture5.PNG

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

PythonCapture6.PNG

Once that's out of the way you'll be on the main dashboard page.

Click on the Files tab.

Create a Python Script

PythonCapture8.png

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

PythonCapture9.png

Then copy and paste this code into the new file:

import datetime

def log_current_time():
# Get the current date and time
now = datetime.datetime.now()
# Format the date and time as a string
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
# Print the timestamp to the terminal
print(f"Task executed at: {timestamp}")
# Write the timestamp to a log file
with open("task_log.txt", "a") as log_file:
log_file.write(f"Task executed at: {timestamp}\n")

if __name__ == "__main__":
log_current_time()


Then select save and then run.

Test the Script in the Console

PythonCapture10.png

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

PythonCapture11.png

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

PythonCapture12.png

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)

PythonCapture13.png

First we will open the script back up and copy the script path.

Navigate to the Tasks Tab

PythonCapture14.png

Then navigate to the Tasks tab and click on it.

Create a Task

PythonCapture15.png

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

PythonCapture16.png

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

PythonCapture17.png

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!

PythonCapture18.png

Open it up and your will see that the script ran on schedule and logged it to the task_log.txt file!

Congratulations!

Congratulations-Pic-2443762323.png

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

  1. A Python website
  2. or build your very own API
  3. Maybe a daily trivia email
  4. 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!