admin管理员组文章数量:1336599
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', '.py?solcast&full&graph', true);
xhr.open('GET', '.py?s=solcast&f=full&g=graph', true);
and
xhr.open('GET', '.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)
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)
Share
Improve this question
edited Nov 19, 2024 at 18:32
Doug Conran
asked Nov 19, 2024 at 17:34
Doug ConranDoug Conran
4771 gold badge5 silver badges20 bronze badges
4
|
1 Answer
Reset to default 1You 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.
- https://redmine.lighttpd/projects/lighttpd/wiki/HowToPythonWSGI
本文标签:
版权声明:本文标题:javascript - How do I pass command line parameters to a python prgram called from XMLHTTPRequest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742409805a2469558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
test.py
script? – Chris Commented Nov 19, 2024 at 17:40