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
17
votes
2 answers

Python SimpleHTTPServer to receive files

I am using SimpleHTTPServer's do_POST method to receive file. The script is working fine if I upload the png file using curl but whenever I use python request library to upload file, File uploads but become corrupt. Here is the SimpleHTTPServer…
john
  • 2,324
  • 3
  • 20
  • 37
16
votes
2 answers

Can one upload files using Python SimpleHTTPServer or cgi?

I would like to have a simple web page on which user can upload files. What would be the simplest way to do it. I know how to start SimpleHTTPServer but I do not know how I can upload files using SimpleHTTPServer. I do not even know if it is…
Roman
  • 124,451
  • 167
  • 349
  • 456
15
votes
6 answers

How to use Content-Encoding: gzip with Python SimpleHTTPServer

I'm using python -m SimpleHTTPServer to serve up a directory for local testing in a web browser. Some of the content includes large data files. I would like to be able to gzip them and have SimpleHTTPServer serve them with Content-Encoding: gzip.…
Jason Sundram
  • 12,225
  • 19
  • 71
  • 86
14
votes
3 answers

Python: How to unit test a custom HTTP request Handler?

I have a custom HTTP request handler that can be simplified to something like this: # Python 3: from http import server class MyHandler(server.BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) …
jakevdp
  • 77,104
  • 11
  • 125
  • 160
14
votes
1 answer

Audio/Video streaming fails using SimpleHTTPServer

I share files in a folder to other devices by invoking a server using python -m SimpleHTTPServer. I just tried to stream videos/audio (standard mp4 & mp3, both under 20MB) to another computer using this & it WORKS (but by throwing the errors…
Jikku Jose
  • 18,306
  • 11
  • 41
  • 61
14
votes
3 answers

How to quiet SimpleHTTPServer?

I have the following simple Threaded fileserver to be used by my application: class FileServer(Thread): """Simple file server exposing the current directory for the thumbnail creator """ def __init__(self, port): …
pistacchio
  • 56,889
  • 107
  • 278
  • 420
13
votes
1 answer

Serve a file from Python's http.server - correct response with a file

I am simply trying to serve a PDF file from the http.server. Here is my code: from http.server import BaseHTTPRequestHandler, HTTPServer class MyServer(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) …
Clone
  • 3,378
  • 11
  • 25
  • 41
13
votes
1 answer

Python SimpleHTTPServer in production

I want to serve static files with Python. Is the Python 3 http.server suitable for use in production? If not, why not? And what are my alternatives?
zxz
  • 709
  • 1
  • 8
  • 19
13
votes
3 answers

Why does SimpleHTTPServer redirect to ?querystring/ when I request ?querystring?

I like to use Python's SimpleHTTPServer for local development of all kinds of web applications which require loading resources via Ajax calls etc. When I use query strings in my URLs, the server always redirects to the same URL with a slash…
Marian
  • 14,759
  • 6
  • 32
  • 44
12
votes
7 answers

Embedded Web Server in Python?

Can you recommend a minimalistic python webserver that I can embedded in my Desktop Application.
Ankur Gupta
  • 2,284
  • 4
  • 27
  • 40
11
votes
1 answer

Processing Simultaneous/Asynchronous Requests with Python BaseHTTPServer

I've set up a threaded (with Python threads) HTTP server by creating a class that inherits from HTTPServer and ThreadingMixIn: class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): pass I have a handler class which inherits from…
Dylnuge
  • 525
  • 1
  • 4
  • 12
10
votes
3 answers

Is it possible to have SimpleHTTPServer serve files from two different directories?

If I do python -m SimpleHTTPServer it serves the files in the current directory. My directory structure looks like this: /protected/public /protected/private /test I want to start the server in my /test directory and I want it to serve files in the…
aw crud
  • 8,791
  • 19
  • 71
  • 115
10
votes
2 answers

Haskell equivalent for python -m http.server?

Is there a way to start a http server to serve static files right from shell using ghc -e or runhaskell ?
Shanthakumar
  • 757
  • 6
  • 23
10
votes
2 answers

Save logs - SimpleHTTPServer

How can I save the output from the console like "192.168.1.1 - - [18/Aug/2014 12:05:59] code 404, message File not found" to a file? Here is the code: import SimpleHTTPServer import SocketServer PORT = 1548 Handler =…
Samantha
  • 383
  • 1
  • 2
  • 12
10
votes
1 answer

CSS not updating on local python httpserver restart

I'm fairly new to the world of web dev and http servers and the like, but I have a basic shell script as follows: PORT=2600 if [[ $1 =~ ^[0-9]+$ ]] then PORT=$1 fi echo "Starting local http server (ctrl-c to exit)" echo "" echo " Demo: …
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
1
2
3
19 20