0

I'm trying (unsuccessfully) to pass command line paramters to a python program called by javascript XMLHTTPRequest. In my python program I have used each of sys.argv, getopt and argparse, all of which work when run from the command line but not when called from the Javascript script. In my Javascript program I have tried:

xhr.open('GET', 'https://solarpredictor.co.uk/test.py?solcast&full&graph', true);
xhr.open('GET', 'https://solarpredictor.co.uk/test.py?s=solcast&f=full&g=graph', true);

and

xhr.open('GET', 'https://solarpredictor.co.uk/test.py?solcast', true);

none of which work. In every case the parameters are not being found by the py program.

How can I get the python program to retrieve parameters when called within XMLHTTPRequest?

For what it is worth, this is my python program:

#!/usr/bin/python3

import sys

solcast = False
full = False
graph = True

print("len = ",len(sys.argv))
for x in range(1,len(sys.argv)):
    param = sys.argv[x]
    print(param)
    if param == 'solcast':
        solcast = not solcast
    elif param == 'full':
        full = not full
    elif param == 'graph':
        graph = not graph

print(solcast,full,graph)
4
  • How are you hosting the test.py script? Commented Nov 19, 2024 at 17:40
  • It's on my own server, solarpredictor.co.uk, and, in fact, in the same directory as the html file. Commented Nov 19, 2024 at 18:28
  • What software are you using on the server? Apache? Nginx? Commented Nov 19, 2024 at 18:53
  • Lighttpd. The point I'd make is that the python program works fine when called through XMLHTTPRequest with no parameters - and also works fine when called with parameters. It is just that it does not receive those parameters. Commented Nov 19, 2024 at 18:57

1 Answer 1

1

You don’t pass command line parameters. You aren’t running the script on the command line.

You have some way of interfacing your HTTP server with your Python code. Last time I looked at Python the way commonly regarded as best was WSGI but other approaches such as mod_python and CGI are available. (Your comments suggest you are using CGI).

The common Python libraries for those interfaces have functions for reading query strings from the request URL.

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

2 Comments

Thank you for that. I need to read the link and then see how to apply it to my case
Thank you! I have now got it working using cgi & cgitb

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.