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

SimpleHTTPServer not found python3

I'm trying to write a simple server in python. So after watching tutorial, I'm trying to import a few modules. from http.server import HTTPServer from http.server import SimpleHTTPServer As the doc says, it has been moved, that's why i'm doing…
Danial
  • 542
  • 2
  • 9
  • 24
9
votes
1 answer

Python SSL server gives me "501 Unsupported method GET"

I've followed this link to build a simple file server with SSL. from http.server import HTTPServer, BaseHTTPRequestHandler import ssl httpd = HTTPServer(('localhost', 4443), BaseHTTPRequestHandler) # openssl req -x509 -newkey rsa:2048 -keyout…
Tomáš Hübelbauer
  • 9,179
  • 14
  • 63
  • 125
9
votes
1 answer

Having Trouble Getting SimpleHTTPRequestHandler to respond to AJAX

I recently tried implementing the SimpleHTTPRequestHandler to accept AJAX requests according to this. Although everything seems to work as far as receiving the request from the client, I cannot send anything back to the client, when I try to…
jab
  • 5,673
  • 9
  • 53
  • 84
8
votes
4 answers

Access SimpleHTTPServer from outside Network

Apache server can be set up and accessed from ouside Network in the following way: http://lifehacker.com/124212/geek-to-live--how-to-set-up-a-personal-home-web-server I want to achieve similar functionality with python SimpleHTTPServer. How is this…
Raghuram Vadapalli
  • 1,190
  • 2
  • 13
  • 27
8
votes
8 answers

How do I kill SimpleHTTPServer from within a Python script?

I am trying to use http.server to test all the links in a Python project. I can get my script to work if I start the server before running my script, and then the server stops when I close the terminal window. But I'd really like the script itself…
japhyr
  • 1,710
  • 2
  • 18
  • 24
7
votes
1 answer

How does python's SimpleHTTPServer do_GET and do_POST functions work?

I've created the following little HTTP server for learning purposes: import SimpleHTTPServer import SocketServer class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): def do_GET(self): print(self.headers) …
MarkMark
  • 185
  • 1
  • 1
  • 7
7
votes
5 answers

Download whole directories in Python SimpleHTTPServer

I really like how I can easily share files on a network using the SimpleHTTPServer, but I wish there was an option like "download entire directory". Is there an easy (one liner) way to implement this? Thanks
awegawef
  • 535
  • 2
  • 5
  • 13
6
votes
0 answers

How to call function from Python HTTP server?

I want to provide one of my applications with a web interface. To achieve that, I subclass BaseHTTPRequestHandler from the BaseHTTPServer package (into a class I call _RequestHandler) and implement my own handlers for HTTP HEAD / GET / POST / PUT…
6
votes
1 answer

Python SimpleHTTPServer 404 Page

I have recently been using Python's SimpleHTTPServer to host files on my network. I want a custom 404 Page, so I researched this and got some answers, but I want to still use this script I have. So, what do I have to add to get a 404 page to this…
SamS
  • 63
  • 1
  • 7
6
votes
4 answers

Python SimpleHTTPServer

Is there a way to make Python SimpleHTTPServer supports mod_rewrite? I'm trying things with Ember.js with leveraging History API as the location API, and to make it work, I have to : 1) add some vhosts config in WAMP (not simple), or 2) run python…
Henson
  • 5,563
  • 12
  • 46
  • 60
5
votes
2 answers

Starting simple python web server in background and continue script execution

I am attempting to start a simple HTTP web server in python and then ping it with the selenium driver. I can get the web server to start but it "hangs" after the server starts even though I have started it in a new thread. from socket import * from…
Kevin
  • 2,852
  • 6
  • 21
  • 33
5
votes
2 answers

How do I implement a simple Captive Portal with `http.server`?

I would like to serve a Captive Portal - page that would prompt the user to agree to certain terms before starting browsing the web from my wireless network (through WiFi) using http.server on my Raspberry Pi that runs the newest version of…
Clone
  • 3,378
  • 11
  • 25
  • 41
5
votes
3 answers

Print statements not working when serve_forever() is called?

I have the following small python script to run a local server for testing some html: print('opened') from http.server import HTTPServer, SimpleHTTPRequestHandler server_address = ('', 8000) httpd = HTTPServer(server_address,…
temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
5
votes
3 answers

How to start python simpleHTTPServer on Windows 10

I recently bought a Windows 10 machine and now I want to run a server locally for testing a webpage I am developing. On Windows 7 it was always very simple to start a HTTP Server via python and the command prompt. Fx writing the below code would…
Kasper Christensen
  • 895
  • 3
  • 10
  • 30
5
votes
2 answers

SimpleHTTPServer: other devices can't connect to the server

Lately I've been playing with Python to discover its potential and I've just stumbled upon SimpleHTTPServer. I'm on Windows 10. I run: python -m SimpleHTTPServer the output is: Serving HTTP on 0.0.0.0 port 8000 ... I've opened the browser both on…
Pigna
  • 2,792
  • 5
  • 29
  • 51
1 2
3
19 20