Have you ever looked at Scratch and thought 'I wish this could be a web server'?
No, neither have I. But I made this anyway.
What is it?
It's a Python script which communicates with Scratch via mesh to enable it to handle web requests. It uses the basic web server which comes with Python with a custom request handler that forwards the data over mesh.
How do I use it?
Python 3 and Scratch with mesh enabled are required to run. The server will use the first two command line arguments as the address and port respectively, or these can be input at runtime. Once launched, connect to mesh on 'localhost' or '127.0.0.1' (uses the default mesh port 42001) and open/create a web server project.
How do I create a server project?
When a request is received, 'request' is broadcast. So you'll need a script starting with
when I receive [request v]The server will wait until it receives the broadcast 'done'. So when you've finished handling the request, you need to use this:
broadcast [done v]The server will reply with the contents of the variable '__content' (two underscores), so you should set this to the HTML source of the appropriate page. The status code sent back will be 200 unless you set the variable '__status' (again two underscores). For example, to send a 'not found' error, you'll need to do this:
set [__status v] to [404]Any variables not beginning with an underscore ('_') will be sent as headers, with the name as the header name and the value as the header value, i.e.
set [Content-Type v] to [image/png]will send the header 'Content-Type: image/png', telling the browser it has received a PNG image. In order to stop variables being sent as headers, precede them with an underscore or choose 'for this sprite only' upon creation. To clear a header, set it to a blank value. (Note: All variables are stored from when they are set until server shutdown, so if your project can sometimes send a 404, it's highly recommended you explicitly set the response to 200 (or whatever) in the other cases, otherwise once one 404 is sent all further requests will end up being 404s, whether the request succeeded or not. This applies to __content and all headers as well.)
Last edited by TheSuccessor (2012-09-08 11:39:41)
Offline
That sounds very cool! I like how easily it integrates with Scratch using sensor variables.
I shall have to try it!
Offline
This could be verry useful, and even more awesome if you use Panther instead of Scratch with file blocks. I can see this comming into use sometime.
Im going to give it a go. And ill let you know if it works because i have an ACTUAL web server (like a $22,000 one ).
Offline
This is brilliant! Nice job!
Do you plan on adding support for cookies or anything like that?
Offline
Magnie wrote:
This is brilliant! Nice job!
Do you plan on adding support for cookies or anything like that?
Thanks
To use cookies at the moment, you can always read/write directly from/to the headers, but I can see that might be awkward. The problem is, lists don't work over mesh, so you'd end up having to send each as a separate sensor. The easiest way would probably be just to provide a sprite file with a few utility scripts in it. I'll think about this for the future.
Offline
TheSuccessor wrote:
Magnie wrote:
This is brilliant! Nice job!
Do you plan on adding support for cookies or anything like that?Thanks
To use cookies at the moment, you can always read/write directly from/to the headers, but I can see that might be awkward. The problem is, lists don't work over mesh, so you'd end up having to send each as a separate sensor. The easiest way would probably be just to provide a sprite file with a few utility scripts in it. I'll think about this for the future.
Or I guess instead of cookies you could do sessions. So the server sets a random cookie ID and then the server tells Scratch what session ID the client is using. Then Scratch can look at whatever internal lists and variables it needs to.
Offline
Magnie wrote:
TheSuccessor wrote:
Magnie wrote:
This is brilliant! Nice job!
Do you plan on adding support for cookies or anything like that?Thanks
To use cookies at the moment, you can always read/write directly from/to the headers, but I can see that might be awkward. The problem is, lists don't work over mesh, so you'd end up having to send each as a separate sensor. The easiest way would probably be just to provide a sprite file with a few utility scripts in it. I'll think about this for the future.Or I guess instead of cookies you could do sessions. So the server sets a random cookie ID and then the server tells Scratch what session ID the client is using. Then Scratch can look at whatever internal lists and variables it needs to.
Oooh, that'd be cool! You could even have the Python script remember all the session variables for each client and set the sensor values accordingly for each request. You could prefix session vars with a symbol like $ or # to tell them apart from the headers
Offline
Epicness!
Offline
Sorry for necropost, but how long did this take you to make? You don't seem to be very enthusiastic about it...
~OrcaCat
Offline
Has anyone still got the python file? I want to try this.
Offline