创建一个简单的HTTP python服务器来处理断开的管道

创建一个简单的HTTP python服务器来处理断开的管道,python,http,server,Python,Http,Server,我创建了一个简单的PythonHTTP服务器,我希望它只显示我想要的目录中的文件,从而改变总是返回“Hello world!”的方式,以及如何处理断管错误?我尝试在那里执行try catch,但我不确定它是否有效: #!/usr/bin/python from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 8089 #This class will handles any incoming requ

我创建了一个简单的PythonHTTP服务器,我希望它只显示我想要的目录中的文件,从而改变总是返回“Hello world!”的方式,以及如何处理断管错误?我尝试在那里执行
try catch
,但我不确定它是否有效:

#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8089

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

    #Handler for the GET requests
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        self.wfile.write("Hello World !")
        return

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

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

except KeyboardInterrupt:
    print '^C received, shutting down the web server'
    server.socket.close()
except socket.error:
    pass
这是我的错误:

someIP - - [25/Dec/2019 09:17:11] "GET someFILE HTTP/1.0" 200 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 293, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 657, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe


下面的示例是创建用于服务静态文件的基本web服务器的基本示例。您可以在代码中找到其他注释,还有一点需要注意:403禁止的实现可以替换为文件索引页,以了解您需要做什么来生成它(根据您的问题,这现在不在范围之内)


我以前也犯过同样的错误

我的代码:

from signal import signal, SIGPIPE, SIG_DFL

signal(SIGPIPE,SIG_DFL)

粘贴在最后,它应该工作

从哪里断了管道?好吧,假设我在服务器上有更多的文件(因此问我如何使目录成为网站的“根目录”),一些用户可能会对某个文件发出请求,他会在收到请求时退出浏览器或其他东西,这将生成一个断开的管道,我的服务器将关闭。@J.Homer您是说ssh上的断开管道吗connection@MohitC我用错误日志更新了这个问题,这样你就可以看到我在说什么了日志中的任何管道错误,以及如何将目录设置为网站的“根目录”?不仅显示“hello world”?@J.Homer:您还通过覆盖do_GET及其self.path来切换根目录的设置。我仍然不完全了解您想要什么,但是上面修改的示例将列出当前文件夹的内容。我想为我的博客制作一个简单的HTTP web服务器,但我对python web服务器等方面没有真正的经验,因此我使用的是“python-m SimpleHTTPServer 8089”为了让一个简单的web HTTP服务器工作,但这确实是不可靠的,我一直在得到上面提到的“断管”错误,并希望找到解决方法。另外,修改后的版本并没有真正发送响应,我的意思是没有显示文件的内容..那么我应该创建什么样的HTTP服务器/?但我的博客将只包含html文件或css等,而不是真正的php或smth elseHello,感谢您的回复!我将如何使用
python-msimplehttpserver
命令实现这一点?还有
SIG_DFL
做什么?
from signal import signal, SIGPIPE, SIG_DFL

signal(SIGPIPE,SIG_DFL)