0

I call my script with http://domain/script.py How can I call the script with a parameter and get the parameter in the python script when I call it like http://domain/script.py?parmameter=value I can't find a solution or even find out if this is possible or not. Would be very happy fo a solution. Thanks to all.


edit: I run it now from php: http://domain/script.php?parmameter=value

<?php
if (isset($_GET['parmameter'])){
    $myval = $_GET['parmameter'];
    $command = escapeshellcmd("python script.py $myval" );
    ...
?>
1
  • Take a look at flask if you do not care about performance. Commented Jul 30, 2016 at 12:15

1 Answer 1

0

Tell us more about your setup and what is working. What is serving your Python script? Are you using a Web server like nginx or Apache?
You can not normally run a Python script via a Web browser without some kind of interface - otherwise you would just get the source of the Python file served as text.
If you were using Flask or Django as a Web framework, then these have built-in ways to access the query strings.

EDIT: since you're calling it as a shell script, you want to get the command line arguments.

Have a look at this answer: Calling command line arguments in Python 3

You can get the value of your $myval variable with: import sys then sys.argv[1] (but best to check that it exists before trying to access it, or use exception handling).

Sign up to request clarification or add additional context in comments.

4 Comments

I am not sure, this is a webhosting with python support (no more infos). I have also SSH access but it is not possible to run python from terminal, only from url.
Right, so you are running it via a shell command essentially, and you want to get the command line arguments.
Were you happy with this answer, @oxidworks? Did it help solve your problem?
no, because I can't run python from command line, only from URL. So I need to use PHP to get args.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.