Sunday, May 15, 2016

Python SimpleHTTPServer

I went and wrote the python simplehttpserver myself, just for kicks...
import http.server
import urllib.parse
import os
class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.log_message("uri: " + urllib.parse.urlparse(self.path)[2])
parsed = urllib.parse.urlparse(self.path)[2].split("/")
# add something to check if it's an api request
# otherwise handle html request...
if '' == parsed[len(parsed) - 1]:
parsed[len(parsed) - 1] = "index.html"
if not '.' in parsed[len(parsed) - 1]:
parsed[len(parsed) - 1] += ".html"
# check if the path exists
p = os.getcwd() + "/".join(parsed)
self.log_message("full path: " + p)
# if it does, f = the file, send good headers
if os.path.isfile(p):
f = open(p, 'rb', 0)
self.send_response_only(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
else:
#file dne, send 400NotFound and 400 response
f = open(os.getcwd() + "/400NotFound.html", 'rb', 0)
self.send_response_only(400)
self.send_header('Content-Type', 'text/html')
self.end_headers()
# send whichever file and the http 'end of message'
self.wfile.write(f.read())
# server address
server_address = ('localhost', 8000)
# how we're going to handle requests
requestHandler = MyRequestHandler #http.server.SimpleHTTPRequestHandler
#requestHandler = http.server.SimpleHTTPRequestHandler
# create our server
server = http.server.HTTPServer(server_address, requestHandler)
# run the server
server.serve_forever()


edit: here's the github

Friday, May 13, 2016

Simple Python Server

This is how you make a simple python server. It'll server index.html if you request the root, or whatever html file you request.
#!/usr/bin/python
import http.server
handler = http.server.SimpleHTTPRequestHandler
serverAddress = ('localhost', 8000)
server = http.server.HTTPServer(serverAddress, handler)
server.serve_forever()

Wednesday, May 11, 2016

Node JS Static Server

I was interested to see how hard it was to write a static file server in pure node js, meaning without express.js. Turns out it's quite easy, but the real point of this was to familiarize myself further with using node and reading the node documentation.
"use strict";
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var server = http.createServer((req,res) => {
console.log("server started");
let filename = 'index.html', route, stat ;
route = url.parse(req.url).pathname;
console.log('Route requested: ' + route);
switch(route){
case '/':
console.log('using default route');
break;
default:
filename = route;
}
console.log('filename: ' + filename);
route = path.join(__dirname, filename);
console.log('checking access to: ' + route);
fs.access(route, (err)=>{
console.log('accessed file, error: ' + err);
if(!err){
fs.readFile(path.join(__dirname, filename),(err, data) => {
console.log('reading file');
let contentType = 'text/' + path.extname(filename).split('.')[1];
console.log('Content-Type: ' + contentType);
res.writeHead(200, {'Content-Type': contentType})
res.write(data);
res.end();
});
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
fs.readFile(path.join(__dirname, 'notFound.html'),(err, data) => {
res.write(data);
res.end();
})
}
});
}).listen(8000);