dqknycyt92288 2017-09-14 14:38
浏览 68

如何从python文件中使用PHP读取变量?

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.

  • 写回答

1条回答 默认 最新

  • dsdsm2016 2017-09-14 14:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值