Category Archives: PythonSharp

PythonSharp (1) A minimal Web Server

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()

def process(context):
    filename = context.Request.Url.AbsolutePath
    if not filename or filename == '/':
        filename = "index.html"
    if filename[0] == '/':
        filename = filename[1:]
    filename = Path.Combine(root, filename)
    if not File.Exists(filename):
    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)


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

The file at repo:

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

New Month’s Resolutions: December 2012

Previous Post

The last month of the year! Time to write my new month’s resolutions. First, let’s review the past month ones:

– Work on PythonSharp [complete] see repo
– Give a talk about Implementing Python at PyCon 2012 (Argentina) [complete] see Spanish post
– Work on AjTalk for Java [pending]
– Work on AjTalk for Javascript [pending]
– Work on BasicScript (I want to use it in an example for my GameServer project) [partial] only demos
– Start .md pages Java tutorial (Spanish) [partial] only browsing/classifying my existing tutorial
– Start .md pages Node.js tutorial [pending]

Instead of AjTalk for Java or Javascript, I was working a lot on my C# version

– Work on AjTalk for C# [complete] see repo

I will gave one-day Node.js course and one poscast, then, these are my new resolutions:

– Work on PythonSharp
– Work on AjTalk for C#
– Give a Node.js course
– Start .md pages Java tutorial

I hope to write posts about the new AjTalk C# features and examples (load/save image, a web server, traits, tests with assert, environments, etc…)

Keep tuned!

Angel “Java” Lopez