如何使用python套接字从html获取输入

Tal*_*mad 3 html python sockets localhost

我有一个代码可以在本地主机上启动一个简单的套接字服务器。但是,我希望它在通过浏览器访问时显示 html 页面。该 html 将包含两个文本字段和提交按钮。当用户在文本字段中输入文本并单击提交按钮时,我希望程序从文本字段中读取内容。我怎样才能做到这一点?

Nab*_*med 5

您的问题的即时答案(我希望如此)在最后一部分 “答案”中-如果我错误地解释了您的问题,请在评论部分告诉我


混乱- 您混淆了基础知识 - 要显示 html 页面,您只需要一个服务器(在您的情况下为 localhost),浏览器将使用 /HTTP协议HTTPS来获取该内容/响应/html 页面。在Python(与其他语言几乎相同)中,对网络服务的访问有两个级别:

  • 低级通过sockets.
  • 通过应用程序级网络协议实现更高级别,例如HTTPFTP等。

套接字- 用简单的英语来说,它是机器操作系统提供的一种功能(接口),用于实现客户端和服务器(对于更高级别的网络协议),例如进程之间的通信。这些进程可以在同一台机器上运行,也可以在物理上分开的机器上运行两个进程(例如,使用浏览器请求网站)。

典型的套接字客户端是(从浏览器请求所需的 html):

client_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_sock.connect(("localhost", 80))
Run Code Online (Sandbox Code Playgroud)

但要使其工作,您必须已经运行服务器(它将提供 html):

server_sock = socket.socket()     # Create a socket object
server_sock.bind((localhost, 80)) # Bind to the port
server_sock.listen(5) 
while True:
     # lines for whatever we want server to do  
Run Code Online (Sandbox Code Playgroud)

该行server_sock.bind((localhost, 80))已将 localhost:80 (套接字基本上是“host_name:port_number”)绑定(分配/分配)到此,server_sock即任何调用/请求都将localhost:80按照上述while True:代码块中的行进行处理,可以是文本响应、HTML 等


典型的网络场景- 我们输入网站名称www.google.com(通过协议解析为 IP DNS- 完全是另一个故事)并按 Enter 键,我们将得到 Google 的搜索主页作为响应 - 这里你是客户端,这在技术上是输入网站名称并按回车键client_sock.connect('www.google.com', 80),它之所以有效,是因为在另一台(远程)机器/系统/主机上它们已经server_socket绑定并侦听即

server_sock.bind('machine's_IP', 80)
server_sock.listen(5)
while True:
    #accept connections from outside
    (client_socket, address) = server_socket.accept()

    # here will be the lines which send us back the Google's 
    # homepage html as a response to our web request.
Run Code Online (Sandbox Code Playgroud)

总而言之,服务器(在你的例子中是网络服务器)可以使用(几乎)任何编程语言来实现,例如Python或C等,但基础,最低层,数据在两个进程之间传递的位置,无论是在同一个进程上使用(环回)的机器或通过( ) 协议locahost在物理上独立的机器/主机上运行的每个进程(典型的 Web 场景)依赖于套接字。套接字是定义、、、协议(所有这些都是 TCP 类型协议)的基本构建块。HTTPTCPHTTPHTTPSFTPSMTP

DNS, DHCP,VOIP协议是UDP协议,但它们也是构建在套接字之上的。


答案- 首先,创建一个 web_server.py 并粘贴以下代码(只是为了看看它是如何工作的)并将该文件作为脚本运行,即从文件的位置运行“python

web_server.py”在命令提示符/终端中:

#!/usr/bin/env python
import socket

host = 'localhost'
port = 80
server_sock = socket.socket(socket.AF_INET,\
                            socket.SOCK_STREAM)    # Create a socket object
server_sock.bind((host , port))                    # Bind to the port

print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)

server_sock.listen(5)             # Now wait for client connection.

print 'Entering infinite loop; hit CTRL-C to exit'
while True:
    # Establish connection with client.    
    client_sock, (client_host, client_port) = socket.socket.accept()
    print 'Got connection from', client_host, client_port
    client_sock.recv(1000) # should receive request from client. (GET ....)
    client_sock.send('HTTP/1.0 200 OK\n')
    client_sock.send('Content-Type: text/html\n')
    client_sock.send('\n') # header and body should be separated by additional newline
    # you can paste your 2 text field html here in the <body>
    client_sock.send("""
        <html>
        <body>
        <h1>Hello World</h1> this is my server!
        </body>
        </html>
    """) 
    client_sock.close()
Run Code Online (Sandbox Code Playgroud)

PS你必须实现一个 HTTP 服务器(网络服务器) - 它肯定会在最低级别使用套接字接口,即

server_sock.bind
server_sock.listen
server_sock.accept 
Run Code Online (Sandbox Code Playgroud)