This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-09-08 11:37:03

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Running Scratch as a web server (scratch2web.py)

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

Request headers and other parameters are sent as sensors. Here's a table:
IP            The remote IP of the client
Port         The remote port of the client
Method    GET, POST, etc.
Host         The host name (e.g. localhost in http://localhost/hello/world.htm)
Path         The path name (e.g. /hello/world.htm in http://localhost/hello/world.htm)
(Blah)       The value of request header (blah)

Why would I choose this over any other web server?

You wouldn't.

Where can I find it?

Here.

Last edited by TheSuccessor (2012-09-08 11:39:41)


/* No comment */

Offline

 

#2 2012-09-08 11:46:35

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

That sounds very cool! I like how easily it integrates with Scratch using sensor variables.  smile

I shall have to try it!


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#3 2012-09-08 11:52:10

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

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  tongue ).


I am currently http://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=imagehttp://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=text and I finally got over 1000 posts.

Offline

 

#4 2012-09-08 12:09:55

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

This is brilliant! Nice job!

Do you plan on adding support for cookies or anything like that?

Offline

 

#5 2012-09-08 12:16:39

TheSuccessor
Scratcher
Registered: 2010-04-23
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

Magnie wrote:

This is brilliant! Nice job!

Do you plan on adding support for cookies or anything like that?

Thanks  big_smile

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.


/* No comment */

Offline

 

#6 2012-09-08 12:54:05

Magnie
Scratcher
Registered: 2007-12-12
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

TheSuccessor wrote:

Magnie wrote:

This is brilliant! Nice job!

Do you plan on adding support for cookies or anything like that?

Thanks  big_smile

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

 

#7 2012-09-08 15:34:04

blob8108
Scratcher
Registered: 2007-06-25
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

Magnie wrote:

TheSuccessor wrote:

Magnie wrote:

This is brilliant! Nice job!

Do you plan on adding support for cookies or anything like that?

Thanks  big_smile

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  smile


Things I've made: kurt | scratchblocks2 | this cake

Offline

 

#8 2012-09-08 15:56:06

djdolphin
Scratcher
Registered: 2010-03-23
Posts: 100+

Re: Running Scratch as a web server (scratch2web.py)

Cool! I tested it and it works!

Offline

 

#9 2012-09-08 17:35:43

bobbybee
Scratcher
Registered: 2009-10-18
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

Epicness!


I support the Free Software Foundation. Protect our digital rights!

Offline

 

#10 2013-04-28 01:50:37

OrcaCat
Scratcher
Registered: 2010-06-30
Posts: 500+

Re: Running Scratch as a web server (scratch2web.py)

Sorry for necropost, but how long did this take you to make? You don't seem to be very enthusiastic about it...

~OrcaCat


     Awesome music     Electrode's theme     Epic music
Bye, 1.x.  sad                             Hello, 2.x!  smile

Offline

 

#11 2013-04-29 01:39:30

nathanprocks
Scratcher
Registered: 2011-04-14
Posts: 1000+

Re: Running Scratch as a web server (scratch2web.py)

Has anyone still got the python file? I want to try this.


http://carrot.cassiedragonandfriends.org/Scratch_Signature/randomsig.php
http://trinary.site40.net/images/scratchrank.php?username=nathanprocks&display=small

Offline

 

Board footer