Questions tagged [simplehttpserver]

SimpleHTTPServer refers to the basic HTTP server which can serve requests with files from the file system with a few lines of code.

An example of using SimpleHTTPServer to serve up an XML document with the /test url parameter using

import SimpleHTTPServer, SocketServer
import urlparse

PORT = 80

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):

       # Parse query data to find out what was requested
       parsedParams = urlparse.urlparse(self.path)
       queryParsed = urlparse.parse_qs(parsedParams.query)

       # request is either for a file to be served up or our test
       if parsedParams.path == "/test":
          self.processMyRequest(queryParsed)
       else:
          # Default to serve up a local file 
          SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);

   def processMyRequest(self, query):

       self.send_response(200)
       self.send_header('Content-Type', 'application/xml')
       self.end_headers()

       self.wfile.write("<?xml version='1.0'?>");
       self.wfile.write("<sample>Some XML</sample>");
       self.wfile.close();

Handler = MyHandler

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

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

Another using [ListenAndServe]2

import ("encoding/xml"; "fmt"; "log"; "net/http")

type xmlresponse struct {
    Message string
    Name string
}

// XML response example
func XmlRequestHandler(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "application/xml")
    w.WriteHeader(http.StatusOK)
    xmlGen := xml.NewEncoder(w)
    xmlGen.Encode(
        xmlresponse{
            Message: "Hi there", 
            Name: req.URL.Query().Get("name")})
}

func main() {
    fmt.Println("Serving files from /tmp on port 8080. Call /testxml?name=someone")
    http.HandleFunc("/testxml", XmlRequestHandler)
    // Else serve from the file system temp directory
    http.Handle("/",  http.FileServer(http.Dir("/tmp")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
296 questions
1619
votes
7 answers

What is the Python 3 equivalent of "python -m SimpleHTTPServer"

What is the Python 3 equivalent of python -m SimpleHTTPServer?
ryanbraganza
  • 16,863
  • 3
  • 17
  • 24
333
votes
15 answers

What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

Python's http.server (or SimpleHTTPServer for Python 2) is a great way of serve the contents of the current directory from the command line: python -m http.server However, as far as web servers go, it's very slooooow... It behaves as though it's…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
312
votes
15 answers

socket.error: [Errno 48] Address already in use

I'm trying to set up a server with python from mac terminal. I navigate to folder location an use: python -m SimpleHTTPServer But this gives me error: socket.error: [Errno 48] Address already in use I had previously open a connection using the…
irm
  • 4,073
  • 6
  • 28
  • 32
255
votes
1 answer

Set up Python simpleHTTPserver on Windows

I want to set up Python SimpleHTTPServer on Windows XP. I have Python installed on my computer. I am executing the following command: python -m SimpleHTTPServer 8888 But I am getting the error: C:\Python33\python.exe: No module named…
codeofnode
  • 18,169
  • 29
  • 85
  • 142
176
votes
6 answers

Enable access control on simple HTTP server

I have the following shell script for a very simple HTTP server: #!/bin/sh echo "Serving at http://localhost:3000" python -m SimpleHTTPServer 3000 I was wondering how I can enable or add a CORS header like Access-Control-Allow-Origin: * to this…
MChan
  • 6,842
  • 27
  • 83
  • 132
121
votes
8 answers

How to run a http server which serves a specific path?

this is my Python3 project hiearchy: projet \ script.py web \ index.html From script.py, I would like to run a http server which serve the content of the web folder. Here is suggested this code to run a simple http server: import…
roipoussiere
  • 5,142
  • 3
  • 28
  • 37
109
votes
3 answers

Is it possible to run python SimpleHTTPServer on localhost only?

I have a vpn connection and when I'm running python -m SimpleHTTPServer, it serves on 0.0.0.0:8000, which means it can be accessed via localhost and via my real ip. I don't want robots to scan me and interested that the server will be accessed only…
user1639431
  • 3,693
  • 8
  • 24
  • 22
74
votes
9 answers

How do I shut down a python simpleHTTPserver?

So I'm trying to learn d3, and the wiki suggested that To view the examples locally, you must have a local web server. Any web server will work; for example you can run Python's built-in server: python -m SimpleHTTPServer 8888 & Great...…
Suz
  • 3,744
  • 3
  • 23
  • 26
68
votes
4 answers

Can I set a header with python's SimpleHTTPServer?

I'm using SimpleHTTPServer to test some webpages I'm working on. It works great, however I need to do some cross-domain requests. That requires setting a Access-Control-Allow-Origin header with the domains the page is allowed to access. Is there…
nynexman4464
  • 1,760
  • 1
  • 17
  • 19
45
votes
2 answers

Reading JSON from SimpleHTTPServer Post data

I am trying to build a simple REST server with python SimpleHTTPServer. I am having problem reading data from the post message. Please let me know if I am doing it right. from SimpleHTTPServer import SimpleHTTPRequestHandler import…
Gatothgaj
  • 1,633
  • 2
  • 16
  • 27
27
votes
5 answers

How do I redirect a request to a different URL in Python

I have been looking for the syntax to redirect a special URL to a remote server to do some XSS testing. import SimpleHTTPServer import SocketServer class myHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): print…
tax
  • 271
  • 1
  • 3
  • 4
23
votes
6 answers

How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?

When I run python -m SimpleHTTPServer 8000 or python -m CGIHTTPServer 8000 in my shell I am hosting the content of my current directory to the internet. I would like to make the following cgi_script.py work correctly using the above command in the…
Bentley4
  • 10,678
  • 25
  • 83
  • 134
21
votes
1 answer

What is the difference between BaseHTTPServer and SimpleHTTPServer? When and where to use them?

What is the difference between BaseHTTPServer and SimpleHTTPServer? When and where should I use these?
Sriram
  • 1,180
  • 2
  • 15
  • 27
18
votes
4 answers

How can I create an local webserver for my python scripts?

I'm looking to use a local webserver to run a series of python scripts for the user. For various unavoidable reasons, the python script must run locally, not on a server. As a result, I'll be using HTML+browser as the UI, which I'm comfortable with,…
Phil H
  • 19,928
  • 7
  • 68
  • 105
18
votes
3 answers

Using SimpleHTTPServer for unit testing

I'm writing a Python module that wraps out a certain web service API. It's all REST, so relatively straightforward to implement. However, I found a problem when it comes to unit testing: as I don't run the services I made this module for, I don't…
Einar
  • 4,727
  • 7
  • 49
  • 64
1
2 3
19 20