Reputation: 6334
here is my python file
import sys
import requests
wrapper = "[Python Storage LogReader v2.b001] "
log = {}
log_lines = sys.argv
index = 0
for line in log_lines[1:]:
error_log = wrapper + line
log[index] = error_log
index += 1
package = {}
package['test1'] = 'A'
package['test2'] = 'B'
package['log'] = log
success = requests.post('http://localhost:3000/post.php', package, False)
I call it like this:
python LogReader.py hello world sample
And the output received as the $_POST
array is this:
Array (
[test1] => A
[test2] => B
[log] => 2
)
Where "2" is the binary count of the arguments passed (I've verified by changing the argument count). What I want is this:
Array (
[test1] => A
[test2] => B
[log] => Array (
[0] => hello
[1] => world
[2] => sample
)
)
I am new to Python but familiar with PHP and the way it parses a $_POST
string. Essentially what I'm looking to send is this:
test1=A&test2=B&log[]=hello&log[]=world&log[]=sample
How do I do this in Python so as to get the equivalent? Obviously my goal is the simplest way possible. Thanks.
Upvotes: 0
Views: 420
Reputation: 2387
Two steps:
log
a list
instead of a dict
:log = [wrapper + log_line for log_line in log_lines[1:]]
or to keep the loop for other purposes:
log = []
for log_line in log_lines[1:]:
log.append(wrapper + log_line)
package
, change the key for log
to log[]
:package['log[]'] = log
By default, requests
would serialize log
(a list) to log=hello&log=world&log=sample
as post data, so formatting it in the way PHP can understand it can be achieved by just adding the empty brackets to the key.
A similar Q&A: https://stackoverflow.com/a/23347265/8547331
Upvotes: 1