Keeps a free PythonAnywhere webapp alive by automating the monthly "Run until 1 month from today" button click.
PythonAnywhere disables free webapps after 1 month unless you log in and click extend. The script does that for you in 4 HTTP calls:
GET /login/— scrape the CSRF token from the inlineAnywhere.csrfToken = "..."script.POST /login/— submitauth-username,auth-password,login_view-current_step=auth, and the token. Session cookies are kept.GET /user/<username>/webapps/— scrape a fresh CSRF token (the post-login one).POST /user/<username>/webapps/<username>.pythonanywhere.com/extend— the actual extend click.
Two flavors:
main.py— Python, readscreds.txt(user=.../pass=...), usesrequests+bs4. Run locally or on cron.extend.gs— Google Apps Script, reads credentials from Script Properties, usesUrlFetchAppwith manual cookie tracking. Free recurring schedule via GAS triggers.
pip install requests beautifulsoup4Create creds.txt next to main.py:
user=yourusername
pass=yourpassword
Run: python main.py — prints Extended successfully on success.
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.
-
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.gsfile. -
Paste the code. Select all the placeholder code in
Code.gsand replace it with the contents ofextend.gsfrom this repo. Click the floppy-disk Save icon (or Ctrl/Cmd+S). Optionally rename the project (top left, "Untitled project") to something likepythonanywhere-forever. -
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.)
- Property
-
Authorize and test. Back in the Editor (the
< >icon), make sureextendPythonAnywhereis 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 seeExtended successfully. -
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(orWeek timerif 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.
- Function:
To check it's still working, revisit the Executions tab (the list icon) — each scheduled run is logged with Extended successfully or Failed: <code>.
web-app.htmlis 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.csrfTokenscript, both scripts will need a tweak — re-save the page and diff againstweb-app.html.