Skip to main content

Reply to "Python code to issue TMCC, Legacy, and PDI Commands"

@cdswindell posted:

The PyTrain API project is up and live on PyPi. You can download it here. The project includes the single executable, pytrain_api, which is all you need to start serving commands. The package includes a Uvicon web server, which launches the PyTrain API on port 8000.

You will need an API Key to make calls. I need to figure out how I want to do this long term (if others download and use the API), as you really don't want someone taking over your layout! For now, use the API key:

e54d4431-5dab-474e-b71a-0db1fcb9e659


This will change, but it will work for now.

Once you install the package (from a virtual environment, of course), hit the URL:

http://<your host ip>:8000/pytrain/v1


to get a list of the supported APIs, You can even try them out from this web page! To do so, hit the green [Authorize] button at the top right of the page and enter the key from above. All of this functionality is provided automagically by FastAPI; really cool stuff!

  -- Dave

Dave,
Not sure if this would help
import secrets
import os

def generate_api_key(length=32):
"""Generates a secure API key."""
return secrets.token_hex(length)

def save_api_key(api_key, filename=".env"): you can change the filename here
"""Saves the API key to a file, handling existing files."""
if os.path.exists(filename):
with open(filename, "a") as f:
f.write(f"API_KEY={api_key}\n")
else:
with open(filename, "w") as f:
f.write(f"API_KEY={api_key}\n")

if __name__ == "__main__":
new_api_key = generate_api_key()
print("Generated API Key:", new_api_key)
save_api_key(new_api_key)
print("API key saved to .env file.") and here

The above code will auto gen an api key results of what it does below

biqu@SBM4P-CB1:~$ python3 api_gen.py
Generated API Key: d51887d41f748d25bd8cc409372cffb7b64683451b81f2e950997e324ca22cc6
API key saved to .env file. will list your filename

Last edited by Shawn_Chronister
×
×
×
×
×