Thursday, 2 January 2020

Keeping server UP with python simpleHTTP server

I am trying to make a blog and sustain it, on this blog the content will only be in html/css or some images, so I don't need any sophisticated web server, I've tried to write a script that will hold the blog active, but sometimes and that's really frequent, it just goes down and I've tried to get logs of it and at first it was something about broken pipe but I tried to fix that using signal handling, and I don't know how to keep the server up all the time. here is the code:

#!/usr/bin/python
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
from os import curdir, sep, path
from signal import signal, SIGPIPE, SIG_DFL
import logging

HOST = #ip_address
PORT = 8089

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        try:
            print(self.path)
            path = self.path
            if ('?' in path):
                path = path.split('?')[0]
            self.path = path
            print(self.path)
            if (self.path == '/'):
                self.path = '/index.html'
            self.path = 'output' + self.path
            if(os.path.isdir(self.path)):
                if(not os.path.exists(self.path + 'index.html')):
                    self.send_response(403)
                    self.wfile.write(str.encode("Listing of directories not permited on this server"))
                else:
                    self.path = self.path + 'index.html'

            f = open(curdir + sep + self.path, 'rb')
            self.wfile.write(f.read())
            f.close()
        except IOError:
            print("File "+self.path+" not found")
            self.send_response(404)


httpd = HTTPServer((HOST, PORT), SimpleHTTPRequestHandler)
httpd.serve_forever()

signal(SIGPIPE, SIG_DFL)


from Keeping server UP with python simpleHTTP server

No comments:

Post a Comment