A python async package to communicate with PythonAnywhere api.
The beta package is available at PyPi.
-
Api Token: get your api token from pythonanywhere account page.
-
Determine if your account is US_Based or EU_Based ( It's commonly US. )
-
Your username.
from mypythonanywhere import AccountType, FriendlyPythonAnywhereClient
client = FriendlyPythonAnywhereClient(
username='MerrilleChoate',
token='API_TOKEN',
account_type=AccountType.UsBased
)
Get cpu usage.
>>> result = await client.cpu.get_cpu_usage()
>>> print(result)
# CpuUsage(daily_cpu_limit_seconds=100, next_reset_time='2022-04-17T11:23:40', daily_cpu_total_usage_seconds=0.0)
Get all of your consoles.
>>> result = await client.consoles.get_consoles()
>>> print(result)
# [Console(id=24036640, user='MerrilleChoate', executable='python2.7', arguments='', working_directory=None, name='Python2.7 console 24036640', console_url='/user/MerrilleChoate/consoles/24036640/', console_frame_url='/user/MerrilleChoate/consoles/24036640/frame/')]
Send requests one by one
import asyncio
async def main():
client = FriendlyPythonAnywhereClient(
username='MerrilleChoate',
token='API_TOKEN',
account_type=AccountType.UsBased
)
consoles = await client.console.get_consoles()
print(consoles)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Send a batch of requests using one session
import asyncio
async def main():
client = FriendlyPythonAnywhereClient(
username='MerrilleChoate',
token='API_TOKEN',
account_type=AccountType.UsBased
)
async with client:
consoles = await client.console.get_consoles()
for x in consoles:
await client.console.kill_console(x.id)
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
from mypythonanywhere.pythonanywhere import PythonAnywhereClient
from mypythonanywhere.types.requests.console_requests import GetConsoleInfo
client = PythonAnywhereClient(
# --- sniff ---
)
console = await client(GetConsoleInfo(123456789)) # Console
🍟 Not all methods are implemented yet! ...
There're api methods that are not implemented. following the order of https://help.pythonanywhere.com/pages/API, methods related to files and below are yet to implement.
Take a look at requests dir for examples of implementing api methods.
All you need is to inherit from BaseRequest[T]
( where T
is the method return type ). then filling the abstract methods, unless you face something new.
Tests are missing for most of methods.