gunslingor
gunslingor

Reputation: 1486

How to read variables using PHP from a python file?

Trying to figure out the best way to do this and I'm getting very odd results. Long story short, we put application config parameters in the backend which is python, the front end PHP used hardcoded parameters and I'm trying to tie that back to the single python file so I don't have to change important config parameters in multiple places, for example to deploy to a different server.

The python file (defaults.py):

import time

#------------------------------------------------------
#Processing Options
#------------------------------------------------------
perform_tests = False
today = time.time()

#------------------------------------------------------
#MySQL Config
#------------------------------------------------------
host = "somehostname.com"
port = 3526
user = "someuser"
pw = "somepass"
db = "someDB"
bulk = 500
table = "Some_table"

The PHP code trying to gather the config variables from it (testing code mostly):

<?php
    function db_connect() {
        $config_file = fopen("../modules/defaults.py", "r");
        if ($config_file) {
            while (($line = fgets($config_file)) !== false) {
                echo "<script>alert(". (string)$line. ");</script>";
                // process the line read.
                if(substr($line, 0, 4) === "host"){
                    //$servername = trim(explode("=",$line)[1]);
                    $servername = "somehostname.com";
                }
            }
            fclose($handle);
        }
        echo "<script>alert(". $servername. ");</script>";
        //$servername = "somehostname.com";
        $username = "someuser";
        $password = "somepass";
        $dbname = "someDB";
        $port = 3526;
        //Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);

        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        return $conn;
    }
?>

As you can see, I'm looping thru the python file trying to reassign my hardcoded variables with the variables from the file... there is a good bit of debugging code in there.

So at the moment when you run it it will alert back to you each line from the file... the VERY odd result is that its not alerting the full line (it's alerting "somehostname.com", 3526, "someuser", etc... it's also not alerting the import time line or the commented out lines. This is very odd to me, I expected it to read the lines as strings but fgets appears to me interpreting the code in a very strange way.

I'd like to know what's really happening here and if there is a better way to go about this... without modifying the python file, because the backend uses it everywhere.

Thanks.

Upvotes: 0

Views: 63

Answers (1)

Kamil
Kamil

Reputation: 11

This looks much like java .properties file. Despite the fact there is some method invoked in line:

today = time.time()

But you can read those properties as string, you can find some help in this question regarding to reading properties files: Read .properties files using PHP

Once you have all data loaded you would have to evaluate lines with code - hopefully you would recognize them by keys. I know this is only general idea, but I hope you can proceed further on your own.

Upvotes: 1

Related Questions