import requests from console import get_consoles_running from file import list_files
def start_console(username, token, executable, arguments, working_directory): # Define the API URL to start a new console url = f'https://www.pythonanywhere.com/api/v0/user/{username}/consoles/'
# Set the authorization header
headers = {'Authorization': f'Token {token}'}
# Define the parameters for starting a new console
console_params = {
"executable": executable,
"arguments": arguments,
"working_directory": working_directory
}
# Send a POST request to start a new console
response = requests.post(url, json=console_params, headers=headers)
if response.status_code == 201:
console_id = response.json()['id']
print(f'Console started successfully. Console ID: {console_id}')
return console_id
else:
print(f'Failed to start the console. Error: {response.status_code} - {response.text}')
return None
def main(): username = 'xxxxxx' api_key = 'xxxxxxxxx' working_directory = '/home/xxx/xxx' executable = 'python3' arguments = '-i /bin/pythonanywhere_runner.py'
# Check and kill existing running consoles
existing_consoles = get_consoles_running(username, api_key)
if existing_consoles != "No running consoles found.":
print (existing_consoles)
for console in existing_consoles:
console_id = console['Console ID']
kill_console(username, api_key, console_id)
# Start a new console
console_id = start_console(username, api_key, executable, arguments, working_directory)
if console_id:
# List files in the specified working directory
path = working_directory
file = list_files(username, api_key, path)
input_text = f'python {file}\n' # Replace 'my_script.py' with your Python file name
# Send input to the console
response_send_input = send_input_to_console(username, api_key, console_id, input_text)
print (response_send_input.text)
if response_send_input.status_code == 200:
print(f'Input sent successfully to run "{input_text.strip()}" in the console.')
# Retrieve the latest console output
url_get_output = f'https://www.pythonanywhere.com/api/v0/user/{{username}}/consoles/{console_id}/get_latest_output/'
headers = {'Authorization': f'Token {api_key}'}
response_get_output = requests.get(url_get_output, headers=headers)
if response_get_output.status_code == 200:
console_output = response_get_output.json()
print('Console output:')
print(console_output)
else:
print('Failed to retrieve console output.')
else:
print(f'Failed to send input to the console.')
def kill_console(username, token, console_id): # Add code to send a DELETE request to kill a console using the API headers = {'Authorization': f'Token {token}'}
url_kill_console = f'https://www.pythonanywhere.com/api/v0/user/{username}/consoles/{console_id}/'
response_kill_console = requests.delete(url_kill_console, headers=headers)
return response_kill_console
def send_input_to_console(username, token, console_id, input_text): # Add code to send input to the console using the API url_send_input = f'https://www.pythonanywhere.com/api/v0/user/{{username}}/consoles/{console_id}/send_input/' headers = { 'Authorization': f'Token {token}' }
data_send_input = {
'input': input_text
}
response_send_input = requests.post(url_send_input, data=data_send_input, headers=headers)
return response_send_input
if name == 'main': main()