Hack Forums
[Python] Error trying to connect to a web with httplib in python? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Error trying to connect to a web with httplib in python? (/showthread.php?tid=6009134)



Error trying to connect to a web with httplib in python? - Avispon99 - Sep 18, 2019

I am trying to connect a python script with a simple website that I made in html and php to test through httplib.


The python script for the connection is as follows:

Code
import http.client as ht
import urllib.parse as ub

openConn=ht.HTTPConnection('127.0.0.1:80')
headers = {'Content-Type':'application/x-www-form-urlencoded','Accept':'text/plain'}
param = ub.urlencode({'usertext':'jhona','passtext':'shaki11'})
openConn.request('GET','htttp://localhost/Test_brut/html_te/php/backdoc.php',param,headers)  
resp=openConn.getresponse()
read_resp=resp.read().decode()
print(read_resp)
clarification: I had to put htttp with triple 't' to be able to make the post since hack forums does not let me put anything similar to link I no longer have 50 posts, so ignore that


The php document that receives the connection is as follows:

Code
$username = $_GET['usertext'];
$password = $_GET['passtext'];

if(isset($username)&& $username=='jhona'){
if(isset($password) && $password == 'shaki11'){

   $open_file = fopen('script_ht.html','r');
   $read_f='';

   while(!feof($open_file)){
       $lin=fgets($open_file);
       $read_f=$read_f.$lin;
   }

   fclose($open_file);

   echo 'WELCOME '.$username.'<br>'.$read_f;
}

else{
   echo "THE PASS IS INCORRECT";
}

}
else{
   echo 'No exist';
}


However, when I try the getresponse() of httplib.client I get the answer the following error:

Code
Undefined index: usertext in on line 4

Undefined index: passtext in on line 5

No exist

I know that the error is in the python script and not in the php document because when I connect from an HTML page entering the user and the password I get the Welcome message, but when I do it from the python script I get the error .

Anyone here know what I am doing wrong?


RE: Error trying to connect to a web with httplib in python? - magicparty - Sep 18, 2019

Why not just touch the url with your parameters hard coded into it?

Code
import requests

usertext = "YOUR STRING"
passtext = "YOUR STRING"

r = requests.get("htttp://localhost/Test_brut/html_te/php/backdoc.php?usertext=%s&passtext=%s" % (usertext, passtext))
print(r.response)

That last part will print [200] by default if the request is successful but the requests library could grab all kinds of things like session cookies etc.