Launching a Python HTTP server from GH Python without blocking

Hi Guys,

Further to my web browser based UI proof of concept thread:

I’ve managed to borrow a Mac Mini from a friend to test/develop for cross platform and I’ve had my first attempts to get a HTML UI up and running.

My code launches a web browser from Rhino fine on MacOS, but the server is not working.

What it looks like the Bengesht http server components are not compatible with MacOS - which is not a surprise as they are really older than the MacOS Rhino.

Not so much of an issue, as I think I can roll my own simple http server in Iron Python.

I have run into a road block tho - when I launch a python http server from a GH Python component both Rhino and Grasshopper cease to be responsive.

By checking the local host port from Chrome I can confirm that the server is running:

Does anyone know a way of launching a python http server from GH Python and having it running in the background? I’ve looked at Threads in Iron Python and this looks like maybe where to head, but getting ‘live’ data from the server might be an issue?

Would love your input if you have some ideas.

Cheers

DK

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()
1 Like

So, I’ve made some progress - I can now launch the http server on it own thread:

I used an idea I saw in this thread:

My worry here is I’m trying to make a MacOS solution and this other post indicates a issue with this method on Rhino/GH for MacOS.

At this stage I now need to find a way to over write the do_GET method in the handler and work out how to get the request headers from this server thread…

Cheers

DK

import rhinoscriptsyntax as rs
import SimpleHTTPServer
import SocketServer
import thread

PORT = 8010

rs.MessageBox("main_thread")

import thread

def my_server(new_port):

    Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    rs.MessageBox("serving at " + str(new_port))
    
    httpd.serve_forever()


id = thread.start_new_thread(my_server, (PORT,))

print id

1 Like