【问题标题】:python server with images带有图像的python服务器
【发布时间】:2014-12-29 18:20:29
【问题描述】:

我正在运行一个加载登录页面的 python 服务器。所有 html 页面都将加载(它们与托管的 html 页面位于同一文件夹中,图像也是如此),但与 html 位于同一文件夹中的图像不会加载。任何人都知道有什么问题图像加载?谢谢。代码如下:

#/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from os import curdir, sep

PORT_NUMBER = 8080

#This class will handles any incoming request from
#the browser 
class myHandler(BaseHTTPRequestHandler):

    #Handler for the GET requests
    def do_GET(self):
        if self.path=="/":
            self.path="login.html"

        try:
            #Check the file extension required and
            #set the right mime type

            sendReply = False
            if self.path.endswith(".html"):
                mimetype='text/html'
                sendReply = True
            if self.path.endswith(".jpg"):
                mimetype='image/jpg'
                sendReply = True
            if self.path.endswith(".gif"):
                mimetype='image/gif'
                sendReply = True
            if self.path.endswith(".js"):
                mimetype='application/javascript'
                sendReply = True
            if self.path.endswith(".css"):
                mimetype='text/css'
                sendReply = True

            if sendReply == True:
                #Open the static file requested and send it
                f = open(curdir + sep + self.path) 
                self.send_response(200)
                self.send_header('Content-type',mimetype)
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
            return


        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)

try:
    #Create a web server and define the handler to manage the
    #incoming request
    server = HTTPServer(('', PORT_NUMBER), myHandler)
    print 'Started httpserver on port ' , PORT_NUMBER

    #Wait forever for incoming htto requests
    server.serve_forever()

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()

【问题讨论】:

  • 可能 Web 服务器未配置为将该文件扩展名路由到您的 Python 处理程序?
  • 哦,图片可能是 png 的?是的,让我检查一下
  • 我修复了 png 问题,谢谢。其中一个输入字段没有加载,但我必须调查一下。不确定是什么原因造成的。

标签: python html


【解决方案1】:

您必须将 mode='rb' 添加到 f = open(curdir + sep + self.path) 因为您正在读取二进制文件。

【讨论】:

    【解决方案2】:

    您可以相应地尝试以下代码来处理 jpg/png 文件

    if self.path.endswith(".jpg"):
                                f = open(applicationPath + '/' + self.path, 'rb')
                                self.send_response(200)
                                self.send_header('Content-type',        'image/png')
                                self.end_headers()
                                self.wfile.write(f.read())
                                f.close()
                                return
    

    【讨论】:

      猜你喜欢
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      • 2022-01-14
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 2017-10-14
      相关资源
      最近更新 更多