无法在 Localhost:port python 上检索 HTML 表单数据 运行
unable to retrieve HTML form data running on Localhost:port python
我运行在我的localhost:port上使用套接字编程进行简单的表单输入。
目前,我的 chrome 上有一个表单 运行,localhost:2333 上只有一个文本框,我可以在我的 wireshark 上看到这样的文本框输入
我输入的输入信息是testesest。
之后,我将 <form action="http://localhost:2333">
放入,以便输入的表单数据可以流回我的 localhost:port
。但是,我的第二个 r= recv(1024)
没有收到任何东西。
import socket
import sys
import os
Addr = ''
PORT = 2333
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((Addr, PORT))
s.listen()
以上为标准件
while(1):
try:
print("waiting for connection")
conn, address = s.accept()
print("New client connected from IP address {} and port number {}".format(*address))
received = conn.recv(1024)
#print("Request received")
#This is what i am hosting
#A webpage with a form
conn.send(b'\r\n')
#This is the webpage content
#The code will stuck here at recv
print("Waiting for form input from client")
r = conn.recv(1024)
print(r.decode())
print("Form input received")
print("HTTP response sent")
except KeyboardInterrupt:
conn.close()
s.close()
conn.close()
s.close()
break
我能得到一些帮助吗?
通过 GET
发送的输入数据附加到 URI
(/?work=<data>
),作为新请求发送:
import socket
import sys
import os
Addr = ''
PORT = 2333
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((Addr, PORT))
s.listen()
while (1):
try:
print("waiting for connection")
conn, address = s.accept()
print(
"New client connected from IP address {} and port number {}".format(
*address
)
)
request = conn.recv(1024)
print("Request received")
method, uri, _ = request.decode().split(' ', 2)
print(method, uri)
#This is what i am hosting
#A webpage with a form
response = ""
conn.send(b'HTTP/1.1 200 OK\r\n')
conn.send(b'Content-Type: text/html\r\n')
conn.send(b'Host: localhost:2333\n')
conn.send(b'\r\n')
if uri == '/':
response = """<html>
<body><form action="http://localhost:2333/" method="GET">
<input type="text" name="work"></form></body>
</html>"""
elif uri.startswith('/?work'):
response = f"<html><body><h2>recevied: {uri[uri.find('/?work=')+7:]}</h2></body></html>"
conn.send(response.encode())
conn.send(b"\r\n")
print("Form input received")
#print("HTTP response sent")
except KeyboardInterrupt:
conn.close()
s.close()
#conn.close()
#s.close()
#break
输出:
waiting for connection
New client connected from IP address 127.0.0.1 and port number 55941
Request received
GET /?work=TestInput
<html><body><h2>recevied: TestInput</h2></body></html>
Form input received
waiting for connection
...
注:
您可能想看看 protocol specs and/or 使用任何现有的库来摆脱这种低级的东西。
每当我们提交任何表单时,浏览器都会发出新的 http 请求,而不是使用现有的连接,因此您需要在新的 http 中处理它 request/connection。
另一件事是,r = conn.recv(1024)
不会让当前连接关闭,这就是为什么在文本字段中按回车键也不起作用的原因。
我运行在我的localhost:port上使用套接字编程进行简单的表单输入。
目前,我的 chrome 上有一个表单 运行,localhost:2333 上只有一个文本框,我可以在我的 wireshark 上看到这样的文本框输入
我输入的输入信息是testesest。
之后,我将 <form action="http://localhost:2333">
放入,以便输入的表单数据可以流回我的 localhost:port
。但是,我的第二个 r= recv(1024)
没有收到任何东西。
import socket
import sys
import os
Addr = ''
PORT = 2333
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((Addr, PORT))
s.listen()
以上为标准件
while(1):
try:
print("waiting for connection")
conn, address = s.accept()
print("New client connected from IP address {} and port number {}".format(*address))
received = conn.recv(1024)
#print("Request received")
#This is what i am hosting
#A webpage with a form
conn.send(b'\r\n')
#This is the webpage content
#The code will stuck here at recv
print("Waiting for form input from client")
r = conn.recv(1024)
print(r.decode())
print("Form input received")
print("HTTP response sent")
except KeyboardInterrupt:
conn.close()
s.close()
conn.close()
s.close()
break
我能得到一些帮助吗?
通过 GET
发送的输入数据附加到 URI
(/?work=<data>
),作为新请求发送:
import socket
import sys
import os
Addr = ''
PORT = 2333
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((Addr, PORT))
s.listen()
while (1):
try:
print("waiting for connection")
conn, address = s.accept()
print(
"New client connected from IP address {} and port number {}".format(
*address
)
)
request = conn.recv(1024)
print("Request received")
method, uri, _ = request.decode().split(' ', 2)
print(method, uri)
#This is what i am hosting
#A webpage with a form
response = ""
conn.send(b'HTTP/1.1 200 OK\r\n')
conn.send(b'Content-Type: text/html\r\n')
conn.send(b'Host: localhost:2333\n')
conn.send(b'\r\n')
if uri == '/':
response = """<html>
<body><form action="http://localhost:2333/" method="GET">
<input type="text" name="work"></form></body>
</html>"""
elif uri.startswith('/?work'):
response = f"<html><body><h2>recevied: {uri[uri.find('/?work=')+7:]}</h2></body></html>"
conn.send(response.encode())
conn.send(b"\r\n")
print("Form input received")
#print("HTTP response sent")
except KeyboardInterrupt:
conn.close()
s.close()
#conn.close()
#s.close()
#break
输出:
waiting for connection
New client connected from IP address 127.0.0.1 and port number 55941
Request received
GET /?work=TestInput
<html><body><h2>recevied: TestInput</h2></body></html>
Form input received
waiting for connection
...
注:
您可能想看看 protocol specs and/or 使用任何现有的库来摆脱这种低级的东西。
每当我们提交任何表单时,浏览器都会发出新的 http 请求,而不是使用现有的连接,因此您需要在新的 http 中处理它 request/connection。
另一件事是,r = conn.recv(1024)
不会让当前连接关闭,这就是为什么在文本字段中按回车键也不起作用的原因。