Right now my Python script is printing data like this:
print(address)
I basically just wanted to send it to a PHP script in the same directory like this:
script.php?data=address
This isn't a publicly accessible server, so I don't think sending a curl request won't work.
The data can be sent as a GET or POST request, whichever would be easiest to implement.
1 Answer 1
Here is an answer that uses that requests library. It is a third party library so you would have to install it pip install requests.
import requests
resp = requests.get("http://localhost/script.php?data=address")
print(resp.text)
If you really wanted to, you can use curl. Just use either localhost or 127.0.0.1 to refer to yourself. The server doesn't have to be publicly accessible, but you do need to be hosting the directory your PHP is in with something like Apache.
If it's not that critical for the data to be sent as a GET or POST request, you can also just call the PHP script directly. subprocess.run(['php', '/path/to/script', 'arg1', 'arg2']), and then in PHP you can access the arguments passed in using the $argv global variable. This method allows you to not have to worry about hosting the PHP code.
2 Comments
AttributeError: 'module' object has no attribute 'run' when trying to use your second option.