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

通过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.

我正在尝试将数据从客户端发送到服务器。 客户端运行一个使用“请求”库的简单python脚本。 服务器端包含另一个使用$ _POST的简单php脚本。 p>

我需要根据客户端程序提供的数据更新网页。 p> \ n

这是python脚本: p>

  import requests 
 
url =“http://xxxxxxx.com/php_files/text_data.php"
ând  = {'test':'它的工作原理!'} 
r = requests.post(url,data = d)
print r.status_code,r.reason 
print r.text 
  code>  pre> \  n 
 

以下是php代码: p>

 &lt;!DOCTYPE = html&gt; 
&lt; html&gt; 
&lt; head&gt; 
&lt; h1&gt;  ; 
&lt;?php 
 
 $ txt = $ _POST ['test']; 
 echo $ txt; 
?&gt; 
&lt; / h1&gt; 
&lt; / head&gt; 
&lt;  / html&gt; 
  code>  pre> 
 
 

我需要php页面显示'It works!' 在h1上,因为这是传递的值。 但由于某种原因,它不显示任何内容 p>

r.text使用'It works!'打印所需的格式。 在&lt; h1&gt; 标签,但实际网站中没有显示。 p>

我也尝试过var_dump($ txt)。 它给了我一个NULL值。 p>

任何帮助都会很高兴。 p> div>

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