duanhan9334 2015-06-15 19:34
浏览 59
已采纳

通过http帖子将数据从客户端发送到服务器

I'm trying to send data from the client to the server. The client runs a simple python script that uses the 'request' library. The server side consists of another simple php script using the $_POST.

I need the webpage to update depending on the data that is given through the client program.

Here is the python script:

import requests

url = "http://xxxxxxx.com/php_files/text_data.php"

d = {'test': 'It works!'}
r = requests.post(url, data = d)
print r.status_code, r.reason
print r.text

And here is the php code:

<!DOCTYPE = html>
<html>
    <head>
        <h1>
            <?php

                $txt = $_POST['test'];
                echo $txt;
            ?>
        </h1>
    </head>
</html>

I need the php page to display 'It works!' on h1 as this is the value that is being passed. But for some reason, it does not display anything

r.text prints the required format with 'It works!' in the < h1 > tags, but the same does not get displayed in the actual website.

I've also tried var_dump($txt). It gives me a NULL value.

Any help would be gladly appreciated.

  • 写回答

1条回答 默认 最新

  • dsxmwin86342 2015-06-15 19:50
    关注

    It seems to me that you are asking a separate instance to update your current instance. The PHP that you are accessing in your browser knows nothing about the python script. It doesn't call the python script at all. In the second session the python script calls the PHP and receives the correct response.

    These are two different sessions, the browser window will see nothing from the python script unless it calls it.

    Here is what is happening:

    Session 1

    1. Run Python script on local machine
    2. Python calls PHP on server
    3. PHP returns output to local machine
    4. Python prints result

    Session 2

    1. Open web browser on local machine
    2. Web browser calls PHP on server
    3. PHP returns results to web browser
    4. Web browser displays results

    There is no persistence in the first session to save the information for the second session. They are two completely separate actions. A more typical way would be to set up a database (or just quick and dirty a text file) on the server to save the information. You need to create a second PHP file to save the information to a database or text file on the server. You then need to modify your previous PHP file to read the information from the database or the text file. The sessions would then be set up the following way.

    Session 1

    1. Run Python script on local machine
    2. Python calls PHP (new file) on server
    3. PHP writes information from python script to database (or text file)
    4. PHP returns status message to local machine
    5. Python prints status

    Session 2

    1. Open web browser on local machine
    2. Web browser calls PHP (original file) on server
    3. PHP reads desired information from database (or text file)
    4. PHP displays information read from server on web browser
    5. Web browser displays results

    If you really want to use the results from the python script in the PHP without a database or text file, you will need to upload the python script to your server, and use one of the methods suggested in Calling Python in PHP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常