Discuss Scratch

ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Chiroyce wrote:

snip
ops
i figured out why my code wont work look at this
Classfied3D
Scratcher
29 posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ninjaMAR wrote:

Chiroyce wrote:

snip
ops
i figured out why my code wont work look at this
Maybe try os.environ instead?
Maximouse
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Raihan142857 wrote:

ninjaMAR wrote:

Chiroyce wrote:

snip
ops
i figured out why my code wont work look at this
Maybe try os.environ instead?
os.getenv() is equivalent to os.environ.get()
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Lazy? Its a horrible idea. Its like going around the whole earth to cross a river.
SansStudios
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Lazy? Its a horrible idea. Its like going around the whole earth to cross a river.

I like the fact that the example code uses asyncio instead of threading. Asyncio is easier to deal with imo
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

SansStudios wrote:

ilcheese2Test wrote:

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Lazy? Its a horrible idea. Its like going around the whole earth to cross a river.

I like the fact that the example code uses asyncio instead of threading. Asyncio is easier to deal with imo
Asyncio is confusing. Threading you technically only need three lines. from threading import Thread a = Thread(target=your_function) a.start()
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Never mind

Last edited by ilcheese2Test (April 12, 2021 23:38:07)

SansStudios
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Asyncio is confusing. Threading you technically only need three lines. from threading import Thread a = Thread(target=your_function) a.start()

I just have no idea how to stop threads correctly lol
Classfied3D
Scratcher
29 posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Lazy? Its a horrible idea. Its like going around the whole earth to cross a river.
Never mind… still, nothing else is working for me and I already knew how to read cloud variables on python, and I literally suggested everything else
ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Maximouse wrote:

Raihan142857 wrote:

ninjaMAR wrote:

Chiroyce wrote:

snip
ops
i figured out why my code wont work look at this
Maybe try os.environ instead?
os.getenv() is equivalent to os.environ.get()
Half the time it just returns null
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

ilcheese2Test wrote:

Classfied3D wrote:

I've found a soloution, involving python ‘talking’ to node.js.
https://replit.com/@Classfied3D/ScratchCloud-installer-V2
It doesn't use websockets, but websockets could look something like this:

import websockets
import asyncio

async def hello(websocket, path):
name = await websocket.recv()
print(f“< {name}”)

greeting = f“Hello {name}!”

await websocket.send(str({“username”: “Classfied3D”, “ProjId”: “513725601”, “method”: “set”, “name”: “☁ test”, “value”: “012345678”}))
print(f“> {greeting}”)

start_server = websockets.serve(hello, “wss/clouddata.scratch.mit.edu/”, 531)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

I'm not really that great at node.js, but someone could read this: https://github.com/trumank/scratch-api/blob/master/scratchapi.js
Your way seems kind of lazy. Its not a very good workaround.
Yeah, it is lazy but it works so…
Lazy? Its a horrible idea. Its like going around the whole earth to cross a river.
Never mind… still, nothing else is working for me and I already knew how to read cloud variables on python, and I literally suggested everything else
Look at my github its somewhere on this topic. Also others seem to be doing the lazy way as well

Last edited by ilcheese2Test (April 13, 2021 22:28:58)

ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

Is changing cloud variables using an external server allowed?
Raihan142857
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

ilcheese2Test wrote:

Is changing cloud variables using an external server allowed?
Yeah, it is. The ST has reviewed projects that did it and they were Safe.
ilcheese2Test
Scratcher
33 posts

How do you set cloud variables in python using websockets?

SansStudios wrote:

ilcheese2Test wrote:

Asyncio is confusing. Threading you technically only need three lines. from threading import Thread a = Thread(target=your_function) a.start()

I just have no idea how to stop threads correctly lol
Stop the thread? I just kill the program ;). The thread isnt a loop. Once if finishes its done the you can start it again if you want. If your function has a loop just use break. Safe meaning they dont stress the server?

Last edited by ilcheese2Test (April 15, 2021 00:23:49)

PikachuB2005
Scratcher
100+ posts

How do you set cloud variables in python using websockets?

I would like to know this as well. Please let me know if you find a way.

Last edited by PikachuB2005 (April 25, 2021 00:58:40)

ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

PikachuB2005 wrote:

I would like to know this as well. Please let me know if you find a way.
Read the whole topic
Classfied3D
Scratcher
29 posts

How do you set cloud variables in python using websockets?

ninjaMAR wrote:

Maximouse wrote:

Raihan142857 wrote:

ninjaMAR wrote:

Chiroyce wrote:

snip
ops
i figured out why my code wont work look at this
Maybe try os.environ instead?
os.getenv() is equivalent to os.environ.get()
Half the time it just returns null
Try this: os.environ (I can’t do straight quotes)
ninjaMAR
Scratcher
1000+ posts

How do you set cloud variables in python using websockets?

Classfied3D wrote:

Try this: os.environ (I can’t do straight quotes)
I figured it out
Classfied3D
Scratcher
29 posts

How do you set cloud variables in python using websockets?

Okay, after my node.js connection thingy that was really lazy, I made a python-only one.
To get vars use ws.recv() in an infinite loop on a thread
My new version: https://github.com/Classfied3D/ScratchCloudV4

Powered by DjangoBB