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

#1 2012-05-26 07:33:18

ARLUMI
New Scratcher
Registered: 2012-03-14
Posts: 9

A Python website connect to local Scratch

Hey guys:

I am trying to make a website using by Python to connect to Scratch(which is on my localhost)

and I hope that can broadcast or receive broadcast...etc. from it .

but, what i try is only get a bad gateway.

Here is my Source Code:

-------------------------------------------------------------
from array import array
import socket
import time
from Tkinter import Tk
from tkSimpleDialog import askstring

root = Tk()
root.withdraw()
import sys

#PORT = 42001
#if HOST == None:
#        sys.exit()
print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#scratchSock.connect((HOST, PORT))
scratchSock.connect(("localhost", 42001))
print("connected!!Waiting for data...")

def sendScratchCommand(cmd):
        n = len(cmd)
        a = array('c')
        a.append(chr((n >> 24) & 0xFF))
        a.append(chr((n >> 16) & 0xFF))
        a.append(chr((n >>  8) & 0xFF))
        a.append(chr(n & 0xFF))
        scratchSock.send(a.tostring() + cmd)

while 1:

        time.sleep(0.01)
        data = scratchSock.recv(1024)
       
        if data == '\x00\x00\x00\x15broadcast "YesLesson"':
                print(data+"1")
        if data == '\x00\x00\x00\x13broadcast "YesExam"':
                print(data+"2")

I use iis in this case.       
--------------------------------------------------------------------------------     
I have a few question:

1.Is it possible to creat this kind of python website and connect to Scratch on localhost?or there's another way like connect Scratch and Python both on the website?

2.I've try to make a easy version like:

-1
--------------------------
import socket

print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect(("localhost", 42001))
print("connected...waiting for data")
---------------------------
-2
--------------------------
import socket

print("connecting...")
sock = socket.socket()
sock.connect(("localhost",42001))
print("connected...waiting for data")
--------------------------
both of them can work when I compiler on localhost
but after I put it on server,it can not work....

Please helllllllllllllllllllllllllllllllllllllllllpppppp me ..... sad

Offline

 

#2 2012-05-26 07:38:05

SJRCS_011
Scratcher
Registered: 2011-02-07
Posts: 1000+

Re: A Python website connect to local Scratch

I don't know any Python, but you can connect to Scratch via it.  However, connecting to your own computer would not be possible unless you have port-forwarded 42001.  Furthermore, in the python code on the website, you would not want it to connect to localhost; that would mean scratch is running  on the website.  The ideal situation would be to have the server on the website, and connect to that via scratch.


http://i.imgur.com/vQqtH.png
Learning to Program in a Nutshell:  "You're missing a closing parentheses" - LS97

Offline

 

#3 2012-05-26 11:12:05

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: A Python website connect to local Scratch

SJRCS_011 wrote:

I don't know any Python, but you can connect to Scratch via it.  However, connecting to your own computer would not be possible unless you have port-forwarded 42001.  Furthermore, in the python code on the website, you would not want it to connect to localhost; that would mean scratch is running  on the website.  The ideal situation would be to have the server on the website, and connect to that via scratch.

Actually, you can access localhost without any port-forwarding because it's just on your machine behind the router.


Posts: 20000 - Show all posts

Offline

 

#4 2012-05-26 11:19:42

SJRCS_011
Scratcher
Registered: 2011-02-07
Posts: 1000+

Re: A Python website connect to local Scratch

veggieman001 wrote:

SJRCS_011 wrote:

I don't know any Python, but you can connect to Scratch via it.  However, connecting to your own computer would not be possible unless you have port-forwarded 42001.  Furthermore, in the python code on the website, you would not want it to connect to localhost; that would mean scratch is running  on the website.  The ideal situation would be to have the server on the website, and connect to that via scratch.

Actually, you can access localhost without any port-forwarding because it's just on your machine behind the router.

but when you put the python on a foreign host, it needs to connect to your computer, so you need to port-forward
What i mean is if the user is on his/her computer, running a scratch server, python on a foreign host cannot connect to it unless his/her router is port-forwarded.

The user also needs a VPS of some sort to run this I think.

Last edited by SJRCS_011 (2012-05-26 11:20:21)


http://i.imgur.com/vQqtH.png
Learning to Program in a Nutshell:  "You're missing a closing parentheses" - LS97

Offline

 

#5 2012-05-26 11:35:42

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

Re: A Python website connect to local Scratch

If I understand what you are trying to do is you want to make a website (in your browser) and then when someone loads that page, it connects to their local Scratch program and then interacts with that.

So, using that method, it won't be possible because that requires client-side Python and nearly no one has that (mostly because of security reasons I'd guess). You'd have better luck with a Java Applet that does that.

Offline

 

#6 2012-05-26 11:37:58

veggieman001
Scratcher
Registered: 2010-02-20
Posts: 1000+

Re: A Python website connect to local Scratch

SJRCS_011 wrote:

veggieman001 wrote:

SJRCS_011 wrote:

I don't know any Python, but you can connect to Scratch via it.  However, connecting to your own computer would not be possible unless you have port-forwarded 42001.  Furthermore, in the python code on the website, you would not want it to connect to localhost; that would mean scratch is running  on the website.  The ideal situation would be to have the server on the website, and connect to that via scratch.

Actually, you can access localhost without any port-forwarding because it's just on your machine behind the router.

but when you put the python on a foreign host, it needs to connect to your computer, so you need to port-forward
What i mean is if the user is on his/her computer, running a scratch server, python on a foreign host cannot connect to it unless his/her router is port-forwarded.

The user also needs a VPS of some sort to run this I think.

Ah, okay.
It seems like he's using some sort of AMP package.
And yeah, but you'll also need to change it to the IP of your computer from localhost and port forward.


Posts: 20000 - Show all posts

Offline

 

#7 2012-09-18 06:19:02

ki59920
New Scratcher
Registered: 2012-08-17
Posts: 3

Re: A Python website connect to local Scratch

Magnie wrote:

If I understand what you are trying to do is you want to make a website (in your browser) and then when someone loads that page, it connects to their local Scratch program and then interacts with that.

So, using that method, it won't be possible because that requires client-side Python and nearly no one has that (mostly because of security reasons I'd guess). You'd have better luck with a Java Applet that does that.

sorry,I just have the question

can u say something how can I do with java applet

or u have java applet source code?

thx ur answer

Offline

 

#8 2012-09-18 09:30:06

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

Re: A Python website connect to local Scratch

ki59920 wrote:

Magnie wrote:

If I understand what you are trying to do is you want to make a website (in your browser) and then when someone loads that page, it connects to their local Scratch program and then interacts with that.

So, using that method, it won't be possible because that requires client-side Python and nearly no one has that (mostly because of security reasons I'd guess). You'd have better luck with a Java Applet that does that.

sorry,I just have the question

can u say something how can I do with java applet

or u have java applet source code?

thx ur answer

I don't have the source for the current applet I use, but the old source is here. You should probably ask MathWizz about anything related to Java.  smile

Offline

 

#9 2012-09-18 16:26:58

SJRCS_011
Scratcher
Registered: 2011-02-07
Posts: 1000+

Re: A Python website connect to local Scratch

Magnie wrote:

ki59920 wrote:

Magnie wrote:

If I understand what you are trying to do is you want to make a website (in your browser) and then when someone loads that page, it connects to their local Scratch program and then interacts with that.

So, using that method, it won't be possible because that requires client-side Python and nearly no one has that (mostly because of security reasons I'd guess). You'd have better luck with a Java Applet that does that.

sorry,I just have the question

can u say something how can I do with java applet

or u have java applet source code?

thx ur answer

I don't have the source for the current applet I use, but the old source is here. You should probably ask MathWizz about anything related to Java.  smile

or me  big_smile
BTW, big necro, might want to report this


http://i.imgur.com/vQqtH.png
Learning to Program in a Nutshell:  "You're missing a closing parentheses" - LS97

Offline

 

Board footer