Coder Social home page Coder Social logo

pyserver's Introduction

Web server Basic Python HTTP web-server application which serves both static and dynamic HTML.

Installation Uses the inbuilt modules HTTPServer, BaseHTTPRequestHandler

Usage from http.server import HTTPServer, BaseHTTPRequestHandler

#import inbuilt module from http library from http.server import HTTPServer, BaseHTTPRequestHandler

#class to hold the server

class WebServer(BaseHTTPRequestHandler): #inbuilt method do_get inherited from the class #method runs when get request is received

def do_GET(self):

    #check path if is a forward slash i.e index page
    if self.path == '/':

        #select path to index.html file
        self.path = '/index.html'

    #try to read the file requested    
    try:
        #read requested file
        file_to_open = open(self.path[1:]).read()



        #200 response is successful
        self.send_response(200)


     #for error if file cant be read   
    except:
        file_to_open = "File not found"

        #404 for file not found
        self.send_response(404)


    self.end_headers()

    #write content of files to screen
    #convert text file to byte
    self.wfile.write(bytes(file_to_open,'utf-8'))

#httpd httpdameion program that runs on background contains the localhost and port

httpd = HTTPServer(('localhost',8000),WebServer)

#the server to run unless closed by user
httpd.serve_forever()

Testing run the script to run server, then open your preffered browser, on the addreess bar type; http://127.0.0.1:8000/ to view website

Contributing Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License MIT

pyserver's People

Contributors

ryhtec avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.