Skip to content

Instantly share code, notes, and snippets.

@di404
Forked from enjalot/cors_server.py
Last active July 2, 2019 10:35
Show Gist options
  • Select an option

  • Save di404/c62c7aff9af9de92b2c39c34563bc9ab to your computer and use it in GitHub Desktop.

Select an option

Save di404/c62c7aff9af9de92b2c39c34563bc9ab to your computer and use it in GitHub Desktop.

Revisions

  1. di404 revised this gist Jul 2, 2019. No changes.
  2. di404 revised this gist Jul 2, 2019. 1 changed file with 9 additions and 12 deletions.
    21 changes: 9 additions & 12 deletions cors_server.py
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    import SimpleHTTPServer
    class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    from http.server import SimpleHTTPRequestHandler
    from http.server import socketserver


    class CORSHTTPRequestHandler(SimpleHTTPRequestHandler):
    def send_head(self):
    """Common code for GET and HEAD commands.
    This sends the response code and MIME headers.
    Return value is either a file object (which has to be copied
    to the outputfile by the caller unless the command was HEAD,
    and must be closed by the caller under all circumstances), or
    None, in which case the caller has nothing further to do.
    """
    path = self.translate_path(self.path)
    f = None
    @@ -48,15 +48,12 @@ def send_head(self):

    if __name__ == "__main__":
    import os
    import SocketServer

    PORT = 31338
    PORT = 8000

    Handler = CORSHTTPRequestHandler
    #Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

    httpd = SocketServer.TCPServer(("", PORT), Handler)
    # Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

    print "serving at port", PORT
    httpd = socketserver.TCPServer(("", PORT), Handler)
    print("server start at:" + PORT)
    httpd.serve_forever()

  3. @enjalot enjalot created this gist Jun 10, 2012.
    62 changes: 62 additions & 0 deletions cors_server.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    import SimpleHTTPServer
    class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def send_head(self):
    """Common code for GET and HEAD commands.
    This sends the response code and MIME headers.
    Return value is either a file object (which has to be copied
    to the outputfile by the caller unless the command was HEAD,
    and must be closed by the caller under all circumstances), or
    None, in which case the caller has nothing further to do.
    """
    path = self.translate_path(self.path)
    f = None
    if os.path.isdir(path):
    if not self.path.endswith('/'):
    # redirect browser - doing basically what apache does
    self.send_response(301)
    self.send_header("Location", self.path + "/")
    self.end_headers()
    return None
    for index in "index.html", "index.htm":
    index = os.path.join(path, index)
    if os.path.exists(index):
    path = index
    break
    else:
    return self.list_directory(path)
    ctype = self.guess_type(path)
    try:
    # Always read in binary mode. Opening files in text mode may cause
    # newline translations, making the actual size of the content
    # transmitted *less* than the content-length!
    f = open(path, 'rb')
    except IOError:
    self.send_error(404, "File not found")
    return None
    self.send_response(200)
    self.send_header("Content-type", ctype)
    fs = os.fstat(f.fileno())
    self.send_header("Content-Length", str(fs[6]))
    self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
    self.send_header("Access-Control-Allow-Origin", "*")
    self.end_headers()
    return f


    if __name__ == "__main__":
    import os
    import SocketServer

    PORT = 31338

    Handler = CORSHTTPRequestHandler
    #Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

    httpd = SocketServer.TCPServer(("", PORT), Handler)

    print "serving at port", PORT
    httpd.serve_forever()