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

#26 2011-05-04 19:34:58

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

Re: Python Network Mirror for Scratch

Well, you found my other topic. Hope it helped.  smile

By the way: I believe you can just ignore the client and just connect Scratch to the Server without any problem since all the mirror does is just take what Scratch sent it and send that data to the server.  smile  Just make sure to change the server port to 42001 instead of 42002 ( I think )

Last edited by Magnie (2011-05-04 19:35:33)

Offline

 

#27 2011-05-04 19:51:30

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

Re: Python Network Mirror for Scratch

well, you mentioned in the other topic that you can do this online. what do I type for join mesh, if that's what you do.(help!!, last question, by the way!! smile  )


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

 

#28 2011-05-04 22:40:42

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

Re: Python Network Mirror for Scratch

Magnie wrote:

This should work for Windows, though I haven't tested it:

Code:

#!/usr/bin/env python

"""
An echo server that uses threads to handle multiple clients at a time.
Entering any line of input at the terminal will exit the server.
"""

import socket
import sys
import threading
from array import array
import time
import re


# Parse Data Definitions
def parseData(str):
    #Check for a broadcast
    e = re.search('broadcast\s\"(.*)\"',str)
    if e:
        #We have a broadcast!
        return 'parsed = broadcastIn("'+e.group(1)+'")'
    #Check for a sensor-update with quoted second value (string)
    e = re.search('sensor-update\s\"(.*?)\"\s\"(.*?)\"',str)
    if e:
        #Got one!
        return 'parsed = sensorUpdateIn("'+e.group(1)+'","'+e.group(2)+'")'
    #Look for a sensor-update with a numeric second value
    e = re.search('sensor-update\s\"(.*?)\"\s([-|\d|.]+)',str)
    if e:
        #Success!
        return 'parsed = sensorUpdateIn("'+e.group(1)+'","'+e.group(2)+'")'

def sensorUpdateIn(var,value):
    #print "Scratch changed "+var+" to: "+value
    return 'sensor-update '+value
    
    
def broadcastIn(broadcast):
    #print "Scratch broadcasted: "+broadcast
    return 'broadcast '+broadcast
# End

class Server:
    def __init__(self):
        self.host = ''
        self.port = 42002
        self.backlog = 5
        self.size = 1024
        self.server = None
        self.threads = []

    def open_socket(self):
        try:
            self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.server.bind((self.host,self.port))
            self.server.listen(5)
        except socket.error, (value,message):
            if self.server:
                self.server.close()
            print "Could not open socket: " + message
            sys.exit(1)

    def run(self):
        self.open_socket()
        input = [self.server,sys.stdin]
        running = 1
        while running:

            c = Client(self.server.accept())
            c.start()
            self.threads.append(c)

        # close all threads

        self.server.close()
        for c in self.threads:
            c.join()

class Client(threading.Thread):
    def __init__(self,(client,address)):
        threading.Thread.__init__(self)
        self.client = client
        self.address = address
        self.size = 1024
        lcs.append([0,0])
        #print lcs
        self.ids = len(lcs)-1
        #print self.ids

    def run(self):
        #print self.address,'has connected.'
        running = 1
        while running:
            data = self.client.recv(self.size)
            if data:
                exec parseData(data)
                if parsed == 'broadcast start': # Start/Setup
                    exe = '''sendScratchCommand('sensor-update "x" "'''+str(lcs[self.ids][0])+'''"')
sendScratchCommand('sensor-update "y" "'''+str(lcs[self.ids][1])+'''"')
'''
                # Movement Start
                elif parsed == 'broadcast up': # Move Up
                    lcs[self.ids][1] += 1
                    #print lcs
                    exe = '''sendScratchCommand('sensor-update "y" "'''+str(lcs[self.ids][1])+'''"')'''
                elif parsed == 'broadcast down': # Move Down
                    lcs[self.ids][1] -= 1
                    #print lcs
                    exe = '''sendScratchCommand('sensor-update "y" "'''+str(lcs[self.ids][1])+'''"')'''
                elif parsed == 'broadcast left': # Move Left
                    lcs[self.ids][0] -= 1
                    #print lcs
                    exe = '''sendScratchCommand('sensor-update "x" "'''+str(lcs[self.ids][0])+'''"')'''
                elif parsed == 'broadcast right': # Move Right
                    lcs[self.ids][0] += 1
                    #print lcs
                    exe = '''sendScratchCommand('sensor-update "x" "'''+str(lcs[self.ids][0])+'''"')'''
                # Movement End

                else: # Unknown Command
                    exe = 'print "Error, no such command."'
                self.client.send(exe)
            else:
                self.client.close()
                running = 0

if __name__ == "__main__":
    lcs = []
    s = Server()
    s.run()

BTW, what does this do?


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

 

#29 2011-05-05 10:32:10

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

Re: Python Network Mirror for Scratch

1. You have to code the Mesh into the Server
2. That is the Windows version of the server.

You should learn a bit of Python before you start making things out of it.  wink

Offline

 

#30 2011-05-05 18:03:06

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

Re: Python Network Mirror for Scratch

Magnie wrote:

1. You have to code the Mesh into the Server
2. That is the Windows version of the server.

You should learn a bit of Python before you start making things out of it.  wink

yeah, I should. just tell me a few things
1) I don't know where to put it in (I have an idea)
2) a windows server for...................


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

 

#31 2011-05-05 18:04:42

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

Re: Python Network Mirror for Scratch

I'll read on it some more.


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

 

#32 2011-05-05 18:49:10

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

Re: Python Network Mirror for Scratch

your example mirror, the one on the front page, I run scratch, then open the mirror, and all python does is opens, writes things too fast for me to see, and closes. I debugged it and found nothing wrong. Did I do something wrong there?


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

 

#33 2011-05-07 09:26:22

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

Re: Python Network Mirror for Scratch

You need to Enable Remote Sensors.  smile  Though the server hasn't been set up for Mesh. It's just a server that you can connect Scratch to. You will need to program whatever you need for the Server yourself.  hmm

Anyways: Basic setup to connect to the Server:
1. Turn on the Server/Run it
2. Open Scratch ( and whatever project you want ) and enable Remote Sensor Connections
3.  Run the Mirror Client.

Offline

 

#34 2011-05-13 21:14:58

gsjs
Scratcher
Registered: 2009-12-24
Posts: 10

Re: Python Network Mirror for Scratch

if only javascript had sockets.  sad  it would make it possible for easy multiplayer 8(

Offline

 

#35 2011-05-15 12:14:52

comp500
Scratcher
Registered: 2010-01-08
Posts: 1000+

Re: Python Network Mirror for Scratch

gsjs wrote:

if only javascript had sockets.  sad  it would make it possible for easy multiplayer 8(

javascript does. its in html5...


800 posts! W00T! Oh sorry im not on a lot but at least i have 1000+ posts

Offline

 

#36 2011-05-21 10:17:09

comp500
Scratcher
Registered: 2010-01-08
Posts: 1000+

Re: Python Network Mirror for Scratch

Magnie wrote:

comp500: That would be interesting to see, if you get one up, could you show it?

markyparky56: What do you mean? Having a Single Player Game on a server, you can save Data there and you can access it from any computer, but Single Player Games off-line can only be played on that computer. But I don't understand very much of what you are talking about.  hmm  Could you explain a little?

Yes but I would need someone to host it for me as i'm not giving out my IP address...


800 posts! W00T! Oh sorry im not on a lot but at least i have 1000+ posts

Offline

 

#37 2011-05-21 23:07:10

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

Re: Python Network Mirror for Scratch

I might be able to, if you put the code somewhere I may be able to run it for a while for everyone's enjoyment ( until I get yelled at for it taking resources, etc ).

Offline

 

#38 2011-05-22 13:54:24

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

Re: Python Network Mirror for Scratch

Magnie wrote:

Though the server hasn't been set up for Mesh

I realize that, but I still call it a mesh.


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

 

#39 2011-05-22 13:57:11

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

Re: Python Network Mirror for Scratch

comp500 wrote:

gsjs wrote:

if only javascript had sockets.  sad  it would make it possible for easy multiplayer 8(

javascript does. its in html5...

I've heard of HTML5. I'm going to use it for the new Kitcat site:). Although, I might switch to xhtml (I just know the !DOCTYPE for xhtml1  sad  ) and I don't even know what version of xhtml is out!


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

 

#40 2011-06-01 18:54:13

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

Re: Python Network Mirror for Scratch

comp500 wrote:

gsjs wrote:

if only javascript had sockets.  sad  it would make it possible for easy multiplayer 8(

javascript does. its in html5...

really? Could you please tell me what they are? I am WAY better at javascript then I am at python, and I don't even know what python is used for!!


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

 

#41 2011-06-02 17:36:06

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

Re: Python Network Mirror for Scratch

never mind, I'll just use PHP


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

 

#42 2012-05-20 14:40:17

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

Re: Python Network Mirror for Scratch

I'm baaaack!
@magnie
I want to make a little program in Visual C#.  I dont know if you know any of it, but I can connect to Scratch, but I want to know how to send data to the remote sensors blocks in Scratch from Visual C#. How, in human words, do you do it in Python? Just say something like, "First, I create an aray, then set a variable, etc.". I'm sure it will be simaler to do in Visual C#.


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

 

#43 2012-05-20 15:34:56

TRocket
Scratcher
Registered: 2009-08-18
Posts: 1000+

Re: Python Network Mirror for Scratch

if you understand java better i wrote the same kind of thing here


http://i.imgur.com/1QqnHxQ.png

Offline

 

#44 2012-05-20 15:41:04

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

Re: Python Network Mirror for Scratch

You could try looking at the Remote Sensors Protocol on the wiki.  smile


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

Offline

 

#45 2012-05-21 00:33:07

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

Re: Python Network Mirror for Scratch

TRocket wrote:

if you understand java better i wrote the same kind of thing here

That actually, suprisingly, helps a lot! Java is very simmaler to C#, so the code makes sense to me.

The wiki doesn't really tell you much. Thats why I posted here.


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

 

Board footer