Angel \”Java\” Lopez on Blog

December 7, 2012

PythonSharp (1) A minimal Web Server

Filed under: Open Source Projects, Programming Languages, Python, PythonSharp — ajlopez @ 4:52 pm

Past month I presented my work on PythonSharp at Argentina PyCon 2012 (see Spanish post), a Python interpreter that I am writing in C#. As in other of my interpreter implementations, it can access native types. As a proof of concept, I wrote a minimal web server example:

from System import Array, Byte
from System.Net import HttpListener
from System.IO import Path, FileStream, FileMode, File

root = "c:/apache-tomcat-6.0.18/webapps/docs"

bytes = Array.CreateInstance(Byte,1024)

listener = HttpListener()
listener.Prefixes.Add("http://*:8000/")

def process(context):
    filename = context.Request.Url.AbsolutePath
    if not filename or filename == '/':
        filename = "index.html"
    if filename[0] == '/':
        filename = filename[1:]
    print(filename)
    filename = Path.Combine(root, filename)
    print(filename)
    if not File.Exists(filename):
        context.Response.Abort()
        return
    input = FileStream(filename, FileMode.Open)
    bytes = Array.CreateInstance(Byte, 1024 * 16)
    nbytes = input.Read(bytes, 0, bytes.Length)
    while nbytes>0:
        context.Response.OutputStream.Write(bytes, 0, nbytes)
        nbytes = input.Read(bytes, 0, bytes.Length)
    input.Close()
    context.Response.OutputStream.Close()

listener.Start()

while True:
    context = listener.GetContext()
    print("new request")
    process(context)
        

The file at repo:

https://github.com/ajlopez/PythonSharp/blob/master/Src/PythonSharp.Console/examples/httpserver.py

To run the example, you should compile the solution. PythonSharp.Console project compiles to pysh.exe console program. You can change the root variable value, now it points to a set of static files in my disk, with Tomcat docs ;-) :

 

The code is based on my previous examples:

A Minimal Http Server In C#

A Minimal Http Server in Java

I can import .NET namespaces and manage them as modules. I’m working with .NET 3.5 (I like to use the minimal requirements) so I could not use other approaches to copy a file to the output response stream, see:

Best way to copy between two Stream instances

Now, I’m working on having the same implementation for my AjTalk Smalltalk virtual machine. But that’s a topic for another post.

Keep tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

1 Comment »

  1. [...] at https://github.com/ajlopez/AjTalk . Some weeks ago, I wrote a simple web server, based on my PythonSharp (1) A Minimal Web Server. The [...]

    Pingback by AjTalk In C# (2): A Simple Web Server « Angel ”Java” Lopez on Blog — December 15, 2012 @ 7:52 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Shocking Blue Green Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 66 other followers

%d bloggers like this: