I had a part of code in php that made a socket and did a post, I'm trying to do it in python but I always get a bad request response. I don't know what am I doing wrong. This is the php code I'm trying to replace:
$data = array(
"id" => $PayloadId,
"source" => $DataSource
);
$post_string = http_build_query($data);
$out = "POST " . $GLOBALS['DATA_PROCESS_PATH'] . " HTTP/1.1\r\n";
$out.= "Host: " . $GLOBALS['DATA_URL'] . "\r\n";
$out.= "Content-type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: " . strlen($post_string) . "\r\n";
$out.= "Authorization: Basic " . base64_encode( $GLOBALS['BASICAUTH_USERNAME'] . ":" . $GLOBALS['BASICAUTH_PASSWORD']) . "\r\n";
$out.= "Connection: Close\r\n\r\n";
$out.= $post_string;
$fp = fsockopen("ssl://" . $GLOBALS['DATA_URL'], 443, $errno, $errstr, 30);
if ($fp) {
fwrite($fp, $out);
fclose ($fp);
when I print the $out in php its:
POST /process.php HTTP/1.1
Host: data.com
Content-type: application/x-www-form-urlencoded
Content-Length: 22
Authorization: Basic xxx
Connection: Close
id=6249198&source=test
what i'm doing in python is:
out="POST /process.php HTTP/1.1\r\n"
out+= "Host: data.com \r\n"
out+= "Content-type: application/x-www-form-urlencoded\r\n"
out+= "Content-Length: 22\r\n"
out+="Authorization: Basic "+ passw+"\r\n"
out+="Connection: Close\r\n\r\n"
out+= "id=6249198&source=test"
port=443
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))
s.send(out.encode())
response = s.recv( 4096 )
print(response)
but I receive bad request response like:
b'HTTP/1.1 400 Bad Request\r\nDate: Tue, 07 Dec 2021 13:02:26 GMT\r\nServer: Apache/2.4.29 (Ubuntu)\r\nContent-Length: 444\r\nConnection: close\r\nContent-Type: text/html; charset=iso-8859-1\r\n\r\n<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n<p>Your browser sent a request that this server could not understand.<br />\nReason: You\'re speaking plain HTTP to an SSL-enabled server port.<br />\n Instead use the HTTPS scheme to access this URL, please.<br />\n</p>\n<hr>\n<address>Apache/2.4.29 (Ubuntu) Server at api.vocsens.com Port 443</address>\n</body></html>\n'
I don't understand what am I doing wrong and how should I disable or add ssl.