python http 网络服务器
我在本地网络上为我的家人创建了一个简单的http服务器,当我添加html文件和png图片并尝试查看HTML文件时,我的图像无法加载。
它说:
“图片“http://...:255/header.png”不能因为它包含错误而被显示。”
这是我的一些代码,
elif self.path.endswith(".bm"): #our dynamic content
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
f= open(curdir + sep + self.path)
ren = self.render(f.read())
self.wfile.write(ren)
return
elif self.path.endswith('.png'):
print "IMAGE WANTED!"
self.send_response(200)
self.send_header('Content-type', 'image/png')
self.end_headers()
f = open(curdir + sep + self.path)
self.wfile.write(f.read())
return
elif self.path.endswith('.jpg'):
print "IMAGE WANTED!"
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
f= open(curdir + sep + self.path)
print f.read()
self.wfile.write(f.read())
return
elif self.path.endswith(".esp"):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write("This Format Is Not Supported Any More, Upgrade To BM Script")
return
除了 png 和 jpeg 部分之外,它们都可以工作。 BM脚本是我自己制作的,与esp相同,所以没什么
I have created a simple http server for my family on the local network, when i add a html file and png picture and tried to view the HTML file, my image cannot load.
It says:
"The image “http://...:255/header.png” cannot be displayed because it contains errors."
Here is a bit of my code
elif self.path.endswith(".bm"): #our dynamic content
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
f= open(curdir + sep + self.path)
ren = self.render(f.read())
self.wfile.write(ren)
return
elif self.path.endswith('.png'):
print "IMAGE WANTED!"
self.send_response(200)
self.send_header('Content-type', 'image/png')
self.end_headers()
f = open(curdir + sep + self.path)
self.wfile.write(f.read())
return
elif self.path.endswith('.jpg'):
print "IMAGE WANTED!"
self.send_response(200)
self.send_header('Content-type', 'image/jpeg')
self.end_headers()
f= open(curdir + sep + self.path)
print f.read()
self.wfile.write(f.read())
return
elif self.path.endswith(".esp"):
self.send_response(200)
self.send_header('Content-type', 'text/plain')
self.end_headers()
self.wfile.write("This Format Is Not Supported Any More, Upgrade To BM Script")
return
They all work except for the png and jpeg section. BM script I made myself, same with esp so that is just nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
open
的默认模式是' r'
,代表读取文本数据并在 Windows 上进行自动 EOL 转换。替换 f = open(curdir + sep + self.path); self.wfile.write(f.read()) withwith
语句修复了文件句柄的泄漏。或者(在 Python <2.5 上),您可以手动调用f.close()
。os.path.join
(为此,您可能需要在文件开头导入 os.path)是比字符串连接更清晰的文件名构造机制。检查生成的文件名是否位于您期望的目录中可以防止路径遍历漏洞< /a> 这将允许任何人读取您系统上的所有文件。The default mode of
open
is'r'
, which stands for reading text data and does automatic EOL conversion on Windows. Replacef = open(curdir + sep + self.path); self.wfile.write(f.read())
withThe
with
statement fixes the leak of file handles. Alternatively (on Python < 2.5), you can callf.close()
manually.os.path.join
(for which you may need toimport os.path
at the beginning of the file) is a cleaner filename construction mechanism than string concatenation. The check that the resulting filename is in the directory you expect prevents the path traversal vulnerability that would allow anyone to read all the files on your system.