I have a php script that runs as cgi on a webserver. The programme is quite simple. First is asks for a userid and password. The userid and password are sent as a parameter. If these value coincide with expected value, the system returns a page where the user may click on a hyperlink to continue his journey.
We thus have a stream with two parameters that is sent from the user browser to the webserver, upon which the system may react.
It is possible to mimick this with a python programme that sends the same stream to the webserver. This programme looks like:
from urllib import request, parse url='http://62.131.51.129/load_pr/password.php' parms = { 'txtUsername' : 'tom', 'txtPassword' : 'binvegni' } querystring = parse.urlencode(parms) u = request.urlopen(url, querystring.encode('ascii')) resp = u.read() print (resp)
This stream is sent to the webserver that runs on http://62.131.51.129. It sends the same stream of bytes as requested from script http://62.131.51.129/load_pr/password.php as it contains two parameter values (’txtUsername’, ’txtPassword’).
We call this Python programme with “D:\Users\tmaanen\Documents>python sendPost.py”. The answer received is:
This matches the stream as sent by the webserver as from the php script.
Finally, in case of get instead of post, the programme is slightly different:
from urllib import request, parse url='http://62.131.51.129/load_pr/password.php' parms = { 'txtUsername' : 'tom', 'txtPassword' : 'bunvegni' } querystring = parse.urlencode(parms) u = request.urlopen(url+'?'+querystring) resp = u.read() print (resp)