【发布时间】:2021-10-01 10:16:15
【问题描述】:
我正在尝试创建一个可以接收文件的 Python Web 服务器。所以有人可以访问该网站,点击表单上的上传按钮,然后文件将被发送到服务器并存储在服务器本地。
这里是index.html的内容
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="8000000" />
<input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>
Server.py 的内容
import socket
class server():
def __init__(self):
self.host_ip = socket.gethostbyname(socket.gethostname())
self.host_port = 81
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.data_recv_size = 1024
def get_data(self, conn):
""" gets the data from client """
data = b""
while b"\r\n\r\n" not in data:
data += conn.recv(self.data_recv_size)
return data
def server(self):
""" main method starts the server """
print(f"[+] Server started listening on port {self.host_port}!")
print(f"[+] Server Ip: {self.host_ip}")
self.s.bind((self.host_ip, self.host_port))
self.s.listen()
while True:
conn, addr = self.s.accept()
with conn:
data = self.get_data(conn)
# GET request
if data[0:5] == b"GET /":
index = open("index.html", "rb").read()
conn.sendall(b"HTTP/1.0 200 OK\nContent-Type: text/html\n\n" + index)
print("[+] Responded to GET request")
# POST request
elif data[0:4] == b"POST":
with open("output.txt", "ab") as file:
file.write(data)
print(f"{len(data)} bytes received from post!")
conn.sendall(b"HTTP/1.0 200 OK\r\nContent-Type: text/html")
s = server()
s.server()
服务器的 GET 部分正常工作,当我访问网站时,index.html 文件显示在我的网络浏览器中,我可以看到文件上传表单。
编辑:我将表单更新为最大文件大小 800 万name="MAX_FILE_SIZE" value="8000000",服务器收到的 POST 响应要大得多(我在下面更新了它),但它看起来仍然不包含文件内容。
POST / HTTP/1.1
Host: 169.254.126.211:81
Connection: keep-alive
Content-Length: 2857323
Cache-Control: max-age=0
Origin: http://169.254.126.211:81
Upgrade-Insecure-Requests: 1
DNT: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryjbf7KaGShYBQ75wT
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://169.254.126.211:81/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,ru;q=0.7
------WebKitFormBoundaryjbf7KaGShYBQ75wT
Content-Disposition: form-data; name="MAX_FILE_SIZE"
8000000
------WebKitFormBoundaryjbf7KaGShYBQ75wT
Content-Disposition: form-data; name="uploadedfile"; filename="IMG_20210131_165637.jpg"
Content-Type: image/jpeg
ÿØÿá„ÙExif MM * @
° ö ¶ ¾POST / HTTP/1.1
Host: 169.254.126.211:81
Connection: keep-alive
Content-Length: 2857323
Cache-Control: max-age=0
Origin: http://169.254.126.211:81
Upgrade-Insecure-Requests: 1
DNT: 1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryjbf7KaGShYBQ75wT
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://169.254.126.211:81/
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,ru;q=0.7
运行脚本时显示 Python IDLE 输出的屏幕截图。
编辑:它只说从 post! 收到了 1024 个字节,所以看起来完整的文件没有被发送。
如何通过 POST 从网络浏览器发送文件,并在服务器上接收文件?
【问题讨论】:
-
我认为您需要增加表单上的最大帖子大小和脚本上的 data_recv_size。内容长度显示为 2804304 字节,但由于大小限制可能不会保存。
-
你在哪里看到
2804304 bytes?当我运行脚本时,它会打印674 bytes received from post! -
它在您的标头响应中 (
Content-Length: 2804304)。您尝试上传的文件是否约为 2.8 MB? -
是的,我正在尝试上传一张 2.8MB 的照片来测试 server.py 是否有效。
-
尝试增加在脚本和上传表单中设置的限制。