Skip to content

flipphoneguy/pythonanywhere-forever

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pythonanywhere-forever

Keeps a free PythonAnywhere webapp alive by automating the monthly "Run until 1 month from today" button click.

How it works

PythonAnywhere disables free webapps after 1 month unless you log in and click extend. The script does that for you in 4 HTTP calls:

  1. GET /login/ — scrape the CSRF token from the inline Anywhere.csrfToken = "..." script.
  2. POST /login/ — submit auth-username, auth-password, login_view-current_step=auth, and the token. Session cookies are kept.
  3. GET /user/<username>/webapps/ — scrape a fresh CSRF token (the post-login one).
  4. POST /user/<username>/webapps/<username>.pythonanywhere.com/extend — the actual extend click.

Two flavors:

  • main.py — Python, reads creds.txt (user=... / pass=...), uses requests + bs4. Run locally or on cron.
  • extend.gs — Google Apps Script, reads credentials from Script Properties, uses UrlFetchApp with manual cookie tracking. Free recurring schedule via GAS triggers.

Python setup

pip install requests beautifulsoup4

Create creds.txt next to main.py:

user=yourusername
pass=yourpassword

Run: python main.py — prints Extended successfully on success.

Google Apps Script setup

Apps Script is Google's free serverless JavaScript runtime — perfect for this since it can fire on a schedule with no server of your own.

  1. Create a project. Go to script.google.com (sign in with any Google account) and click New project (top left). You'll get an editor with an empty Code.gs file.

  2. Paste the code. Select all the placeholder code in Code.gs and replace it with the contents of extend.gs from this repo. Click the floppy-disk Save icon (or Ctrl/Cmd+S). Optionally rename the project (top left, "Untitled project") to something like pythonanywhere-forever.

  3. Store your credentials. Click the gear icon (Project Settings) in the left sidebar. Scroll down to Script Properties and click Add script property. Add two:

    • Property PA_USERNAME, Value: your PythonAnywhere username
    • Property PA_PASSWORD, Value: your PythonAnywhere password

    Click Save script properties. (Storing them here keeps them out of the source code, so it's safe to share.)

  4. Authorize and test. Back in the Editor (the < > icon), make sure extendPythonAnywhere is selected in the function dropdown at the top, then click Run. The first run pops up a permissions dialog — choose your Google account, click Advanced → Go to (unsafe) (Google warns this for any unverified personal script), then Allow. After it finishes, open the Execution log at the bottom — you should see Extended successfully.

  5. Schedule it. Click the clock icon (Triggers) in the left sidebar, then Add Trigger (bottom right). Set:

    • Function: extendPythonAnywhere
    • Event source: Time-driven
    • Type: Month timer (or Week timer if you'd rather it run weekly for safety)
    • Day/time: anything — e.g. 1st of the month, 3am

    Click Save. From now on Google runs it for you on that schedule.

To check it's still working, revisit the Executions tab (the list icon) — each scheduled run is logged with Extended successfully or Failed: <code>.

Notes

  • web-app.html is a saved copy of the logged-in webapps page used as a reference for selectors / endpoint shape. Not needed at runtime.
  • If PythonAnywhere changes the login form fields or the Anywhere.csrfToken script, both scripts will need a tweak — re-save the page and diff against web-app.html.

About

Auto-extend free PythonAnywhere webapps. Python + Google Apps Script.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors