I need to send a simple form to a remote server using POST method with python. This form is a bit strange... some fields name are repeated without using the [] but only passing the key=value pair (this is a requisite otherwise the process on server side doesn't work!).
Below an example of the original form:
<form method="post" action="/remote/test.php">
<input type=hidden name="value01" value="7777" size="12" maxlength=20>
<input type="text" name="value02" value="123456789">
<input type="text" name="value03" value="A1234">
<input type="text" name="value04" value="B5678">
<input type=submit value="value05 " class="button">
<input type="hidden" name="VALUE00" value="9999">
<input type=hidden name="VALUE99"value="09.01.2015 14:52:40">
<input type="hidden" name="LIST" value="D0000000033039">
<input type="hidden" name="LIST" value="D0000000033039">
<input type="hidden" name="LIST" value="C0000000032032">
<input type="hidden" name="LIST" value="X0000000031820">
</form>
I use "Requests" and as per the documentation, i can pass a dictionary for sending post parameters. So i implement below code. The real data that fill the sourceDataList variable are read from a text file with a windows EOL (CR+LF), so to "emulate" the file for this example, i compile a list with some value similar to real values that are present in the file
import re
sourceDataList = [
'2017-12-20 08:59:17;Value01\r\n',
'2017-12-20 08:59:18;Value02\r\n',
'2017-12-20 08:59:20;Value03\r\n',
'2017-12-20 08:59:21;Value04\r\n'
]
dataList = [];
for line in sourceDataList:
dataList.append({'RESULT' , re.sub('[^A-Za-z0-9]+', '', line.split(';')[1].strip())});
print dataList;
The problem start here.. when I print the dataList I discover that the data are "mixed" in a strange way.. sometime there is a swap between key and value!
as example, the code above produce below result:
[
set(['Value01', 'RESULT']),
set(['RESULT', 'Value02']),
set(['RESULT', 'Value03']),
set(['Value04', 'RESULT'])
]
where did i go wrong?
- UPDATE 1 -
my purpose is to create a dictionary that i can use with "Requests". If i use above set the call work as expected and send the data to server but in wrong order (some key and value inverted)
If i use a real dictionary like this (notice that now i use : instead , ):
for line in sourceDataList:
dataList.append({'RESULT' : re.sub('[^A-Za-z0-9]+', '', line.split(';')[1].strip())});
and use it as parameter for "requests":
r = requests.post(url, data=dataToSend);
I get an error:
File "test.py", line 46, in <module>
r = requests.post(url, data=dataToSend);
File "C:\Python27\lib\site-packages\requests\api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Python27\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 494, in request
prep = self.prepare_request(req)
File "C:\Python27\lib\site-packages\requests\sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "C:\Python27\lib\site-packages\requests\models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "C:\Python27\lib\site-packages\requests\models.py", line 499, in prepare_body
body = self._encode_params(data)
File "C:\Python27\lib\site-packages\requests\models.py", line 97, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: need more than 1 value to unpack
dataListto look like ?