Knowledgebase

Interacting With the Operating System Print

  • python, webhosting, guide, howto, solution, zillionkinghost, hosting, support
  • 0

System tasks.

WHAT PYTHON CAN DO

Read environment variables List and manipulate files Run other programs Report system information

WHEN TO RUN ANOTHER PROGRAM

When a tool already does the job well.

WHAT TO USE

The subprocess facilities, rather than older or simpler alternatives.

WHAT TO NEVER DO

Build a command by joining strings containing values from outside.

WHY

That is command injection, and it gives the caller control of your server.

WHAT TO DO INSTEAD

Pass the command and its arguments as a list, without a shell.

WHAT TO ALWAYS SET

A timeout, so a hung command does not hang your script.

WHAT TO CAPTURE

Output and exit status.

WHAT TO CHECK

The exit status, before assuming success.

WHAT TO HANDLE

The command not existing The command failing Unexpected output

WHAT TO BE CAREFUL WITH ON SHARED HOSTING

Commands requiring privileges you do not have Resource-intensive operations

WHAT TO PREFER

A Python library over running an external command, where one exists.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot