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

Python: run SimpleHTTPServer and make request to it in a script

I would like to write Python script that would: Start SimpleHTTPServer on some port Make a request to the server and send some data via POST Run script via shell in interactive mode and interact with it Right now the code looks like this: import…
0leg
  • 13,464
  • 16
  • 70
  • 94
5
votes
3 answers

Python's SimpleHTTPServer started in a thread won't close the port

I have the following code: import os from ghost import Ghost import urlparse, urllib import SimpleHTTPServer import SocketServer import sys, traceback from threading import Thread, Event from time import sleep please_die = Event() # this is my…
Tiberiu C.
  • 3,365
  • 1
  • 30
  • 38
5
votes
1 answer

Python SimpleHTTPServer hangs after several requests

I'm writing a simple integration test which consists of the following: Starting a simple HTTP server which serves up a page to be tested Starting various different browsers which load this page to confirm that everything works. I know #2…
5
votes
3 answers

Port 8000, PID 4, not able to kill, taskill /f /pid 4, Access Denied

I am not able to kill process bound to 8000 port, due to which I am not able to start HTTP server. This is in reference to question Start HTTP/HTTPS server, python -m SimpleHTTPServer C:\>taskkill /f /pid 4 ERROR: The process with PID 4 could not…
garg10may
  • 5,794
  • 11
  • 50
  • 91
5
votes
3 answers

BasicHTTPServer, SimpleHTTPServer and concurrency

I'm writing a small web server for testing purposes using python, BasicHTTPServer and SimpleHTTPServer. It looks like it's processing one request at a time. Is there any way to make it a little faster without messing around too deeply? Basicly my…
braindump
  • 309
  • 2
  • 4
  • 13
5
votes
8 answers

Real time embeddable http server library required

Having looked at several available http server libraries I have not yet found what I am looking for and am sure I can't be the first to have this set of requirements. I need a library which presents an API which is 'pipelined'. Pipelining is used to…
Howard May
  • 6,639
  • 9
  • 35
  • 47
5
votes
2 answers

LSOpenURLsWithRole() errors when in a tmux session

LSOpenURLsWithRole() failed with error -600 for the URL http://localhost:9000/. This is the error I get when I try to launch my SimpleHTTPServer while in a tmux session. I'm a front-end web developer and I spend most of my time working with a…
Adrian Oprea
  • 2,360
  • 3
  • 21
  • 23
5
votes
1 answer

How to change the default 404 page while using python SimpleHTTPServer

When I start a http server using a command: python -m SimpleHTTPServer How can i change the default 404 page?
kino lucky
  • 1,365
  • 4
  • 15
  • 24
5
votes
1 answer

How to handle TIdHTTPServer with TIdMultiPartFormDataStream

Hi i need help on how to retrieved the parameters and data using IdHttpServer from indy. many of my application uses TIdMultiPartFormDataStream to send data over the php. I would like to use the TIdHTTPServer to verify parameters for some reason and…
XBasic3000
  • 3,418
  • 5
  • 46
  • 91
5
votes
1 answer

Jersey restful service communication (IncompatibleClassChangeError)

I`ve created a restful service facade based on jersey 1.12 on the JDK 1.6 http server. When I start my application in eclipse everything works fine. I can communicate with the facade without any troubles but when I start my application via the…
martin s
  • 1,121
  • 1
  • 12
  • 29
4
votes
1 answer

Loading module from was blocked because of a disallowed MIME type (“”)

I am going through a three.js example and when I use import within a javascript module I get the error: Loading module from “http://localhost:8000/build/three.module.js” was blocked because of a disallowed MIME type (“”). When I import traditionally…
Null Salad
  • 765
  • 2
  • 16
  • 31
4
votes
0 answers

Python3 SimpleHTTPServer error

I am using SimpleHTTPServer in Python 3.4.3 python -m http.server When I try to do a request from my client scrypt def download_file(url): local_filename = path_leaf(url) try: r = requests.get(url, stream=True, timeout=6) …
Hrvoje T
  • 3,365
  • 4
  • 28
  • 41
4
votes
1 answer

Force reload on SimpleHTTP Server in Python

I have a very simple HTTPServer implemented in Python. The code is the following: import SimpleHTTPServer import SocketServer as socketserver import os import threading class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): path_to_image =…
Kev1n91
  • 3,553
  • 8
  • 46
  • 96
4
votes
1 answer

How to get javascript to parse json.gz file on browser

In a nutshell - Essentially I am trying to create a Heavy-client & Light-Server Single Page application. I am GET-ing a compressed JSON data file via a simple HTML script tag.
anu
  • 1,017
  • 1
  • 19
  • 36
4
votes
3 answers

Pass Parameter to SimpleHTTPRequestHandler

I have to pass a parameter to SimpleHTTPRequestHandler class, so I used class factory to create a custom handler as below. def RequestHandlerClass(application_path): class CustomHandler(SimpleHTTPRequestHandler): def __init__(self,…