以下代码部分中的 user_input = post_data.split('=')[1] 是什么?

问题描述 投票:0回答:1

我正在编写一段Python代码来处理来自html服务器的POST请求方法。下面的代码块是在我之前提出的问题的答案中提供给我的。我已经逐行浏览了它,以确保我理解这里的逻辑。但我在这条线上特别遇到了障碍。

user_input = post_data.split('=')[1]

因此,我将概述我认为逻辑正在做什么,然后提供实际的代码,我希望有人可以在适当的情况下纠正我,以确保我真正正确理解逻辑。并进一步解释为什么上面指出的代码行根本存在,我不明白为什么在这种情况下需要 split 方法。

post_data 不能简单地按原样打印出来吗?或者这会因为某种原因导致问题吗?

我在这里提供上下文的顶部部分,因为它是程序的其余部分。

# Python 3 server example
from  http.server import BaseHTTPRequestHandler, HTTPServer 
import time #Why is time imported if it's not used? Hypothesis: the send response method on line 10 states among other things to send the current date. Thus time is needed to determine current date?

hostName = "localhost"
serverPort = 8080

class MyServer(BaseHTTPRequestHandler): 
    def do_GET(self): 
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("<html><head><title>https://pythonbasics.org</title></head>", "utf-8"))
        self.wfile.write(bytes("<p>Request: %s</p>" % self.path, "utf-8"))
        self.wfile.write(bytes("<body>", "utf-8"))
        self.wfile.write(bytes("<p>Hello world! This is a webpage!</p>", "utf-8"))
        self.wfile.write(bytes("<p> And hello to you! Please enter your name below!</p>", "utf-8"))
        self.wfile.write(bytes("""<form action="/" method="post">
                                   <label for="name">Please enter your name:</label><br>
                                   <input type="text" id="name" name="name"><br>
                                   <input type = "submit" value = "Click me!">
                                </form>  
                              """, "utf-8" )) 
        self.wfile.write(bytes())
        self.wfile.write(bytes("</body></html>", "utf-8"))

下面的这部分是我想验证我的理解的代码部分。我认为这里发生的事情符合逻辑,并且代码中的注释中包含了一些其他问题。

def do_POST(self): 
        content_length = int(self.headers['Content-Length']) 
        #The content-string above is an html header responsible for declaring the length of the text being passed to the server via the POST method. 
        #However, Content-Length is never declared a value that I can tell, not here or in the rest of the program. So, does HTML then by default take the entire doccument provided by the POST method if Content-Lengtg is not assigned any value?
        # It is then coerced into an int and passed to the content_length variable. But what is the purpose of the ".headers" method?
       
        post_data = self.rfile.read(content_length).decode('utf-8') 
        #utf-8 is the unicode format for encoding/decoding the given text. .read is being passed the length of the message and thus is reading the entire message. The message is then passed to .rfile, I am not sure why this is, why is the standard .read method not sufficent?
        
        user_input = post_data.split('=')[1] 
        #I really have no idea why this the split method is needed at all. Could post_data not be used as it is?

        self.send_response(200) 
        #Sends the webpage the 200 response code indicating the server proccessed the request correctly.
        self.send_header('Content-type', 'text/html')
        #Declares that the data about to be sent is of the type text, and is written in HTMl
        self.end_headers()

        self.wfile.write(bytes("html>head>title>https://pythonbasics.org</title>/head>", "utf-8"))
        #I don't understand the use of the bytes class in these lines. I'm assuming that html needs information passed to it to be encoded into bytes for the transfer? This class does so?
        self.wfile.write(bytes("<body>", "utf-8"))
        self.wfile.write(bytes(f"<p>Hello {user_input}!</p>", "utf-8"))
        #At the beginning of this string after the bytes class ther is a single "f" present. Why is this? Is it something to do with html coding or python? Also am I right in thinking that {user_input} is the syntax in HTML for inserting a variable?
        self.wfile.write(bytes("</body></html>", "utf-8"))
python html python-3.x server logic
1个回答
0
投票

原因是因为post_data变量中包含“name=”,那么如果你想要文本输入值;你必须分割字符串并得到最后一部分。

© www.soinside.com 2019 - 2024. All rights reserved.