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
0
votes
1 answer

Bash Function in .profile is not running SimpleHTTPServer - file not found

So I have a simple bash function stored in ~/.profile as follows: function testingServer() { local port="${1:-8000}" python SimpleHTTPServer "$port" open "http://localhost:${port}/" } But when I run the function, it gives me…
JayGee
  • 577
  • 6
  • 16
0
votes
1 answer

pyinstaller and simplehttpserver on OSX vs WIN

I've made a simple server based on another thread here on SO: Shutting down python TCPServer by custom handler I wrapped this up into an .exe using pyinstaller like so: pyinstaller C:\PATH TO FILE\server.py --distpath=\PATH TO FILE\ --onedir…
Luis E. Fraguada
  • 509
  • 8
  • 24
0
votes
2 answers

Configuring SimpleHTTPServer to assume '.html' for suffixless URLs

How can I configure the Python module "SimpleHTTPServer" such that e.g. foo.html is opened when e.g. http://localhost:8000/foo is called?
Tohuw
  • 3,630
  • 5
  • 22
  • 24
0
votes
1 answer

python simple server with 3.4.2

I'm not savvy with Python or server programming at all. My AVG blocked Python from running SimpleHTTPServer. I was able to install Python 3.4.2 successfully, but noticed that SimpleHTTPServer has been moved into HTTP server. How can I set up my…
kirdua
  • 95
  • 1
  • 2
  • 8
0
votes
1 answer

XMLHttpRequest No 'Access-Control-Allow-Origin' header From SimpleHttpServer

I am writing a rails backend app. I am providing a rest api to the frontend developer who is workng separately. So for time being I have enabled Cross origin resource sharing by adding following in my application.rb: #todo remove this once ui is…
septerr
  • 6,445
  • 9
  • 50
  • 73
0
votes
3 answers

Python webserver script won't submit to sqlite3 database

In the below script a simple webserver is created and there is a class that should sends GET data to a sqlite3 database. import SimpleHTTPServer import SocketServer import sqlite3 PORT = 8000 Handler =…
metersk
  • 11,803
  • 21
  • 63
  • 100
0
votes
1 answer

Python SimpleHttpServer howto

I have: I created index.html - the simplest html page & launch my simple http server with python -m SimpleHttpServer 8000. I want: make index.html use javascript which depends on JQuery. Problem: Seems SimpleHttpServer doesn't load JS files. I mean…
VB_
  • 45,112
  • 42
  • 145
  • 293
0
votes
2 answers

How to set up AJAX path to SimpleHTTP server using GET

I have a string that is stored in a variable, request_str, and I want to pass that data to a SimpleHTTP python web server. I am unsure how to go about actually connecting the AJAX that I have to the simpleHTTP server. Here is the ajax I've set up…
0
votes
1 answer

Python SimpleHttpServer Background bug?

Using a very simple BaseHttpServer (code below) I am experiencing the following problem: Starting the server in background using ./testserver.py & works fine and the server responses on port 12121 . When I type in disown the server still responses.…
ProfHase85
  • 11,763
  • 7
  • 48
  • 66
0
votes
2 answers

Issue with Python Server Returning File On GET

I created a simple threaded python server, and I have two parameters for format, one is JSON (return string data) and the other is zip. When a user selects the format=zip as one of the input parameters, I need the server to return a zip file back…
code base 5000
  • 3,812
  • 13
  • 44
  • 73
0
votes
1 answer

Font awesome not loading when using Python simple http server

I have this simple html file that displays the thumbs down icon using font awesome.