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

#26 2012-04-28 04:19:33

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Also I have a question, since i'm going to be getting a dedicated server soon, I want to know howto make the server send and recieve broadcasts, and add them to a list.


I'm back.
Maybe.

Offline

 

#27 2012-04-28 04:59:55

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

just to point out - according to IDLE, this line is invalid:
# close all threads

Ln: 54, Col: 8


I'm back.
Maybe.

Offline

 

#28 2012-04-28 09:07:56

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

DigiTechs wrote:

just to point out - according to IDLE, this line is invalid:
# close all threads

Ln: 54, Col: 8

If you could give the full error, that would be better. Also, are you using Python 3.x or 2.x or are you using the EXE?

Code:

#!/usr/bin/env python

"""
This is a Mesh Server for Scratch (and possibly any project)
that you can run in the background on a VPS of the sorts.

It's totally insecure and anyone can connect and attempt to
send messages (whether broadcasts or sensor-updates) to the
users connected.

This version supports Windows! :D
"""

import select
import socket
import sys
import threading

class Server:
    def __init__(self):
        self.host = ''
        self.port = 42001
        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()
        running = 1
        while running:
            
            try:
                c = Client(self.server.accept())
                c.start()
                self.threads.append(c)
            
            except KeyboardInterrupt:
                junk = sys.stdin.readline()
                running = 0

        # 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

    def run(self):
        running = 1
        while running:
            try:
                data = self.client.recv(self.size)
            except Exception, e:
                self.log(str(e))
                data = ''
            
            if data:
                for user in s.threads:
                    if user == self:
                        continue
                    
                    try:
                        user.client.send(data)
                    except Exception, e:
                        self.log(str(e))
            
            else:
                self.client.close()
                running = 0
    
    def log(self, string):
        try:
            log_file = open("mesh.log", 'a')
            log_file.write('\n' + string)
            log_file.close()
        except IOError, e:
            log_file = open("mesh.log", 'w')
            log_file.write(string)
            log_file.close()

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

Offline

 

#29 2012-04-28 09:13:45

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Magnie, I'll try to fix the bug in the java server this coming Thursday.
Was the client that originated the messages receiving the echo ok?


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

Offline

 

#30 2012-04-28 09:37:53

ProgrammingFreak
Scratcher
Registered: 2010-09-04
Posts: 1000+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Could you explain more about how this can be configured?
I'm kinda new to python.

Offline

 

#31 2012-04-28 13:03:25

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

SJRCS_011: It didn't work on the originating client either.

ProgrammingFreak: What do you mean? It shouldn't need any configuring. It just needs Python 2.x to run. Everything else is already configured to support Scratch Mesh connections.  smile

Offline

 

#32 2012-04-29 03:42:45

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Magnie wrote:

DigiTechs wrote:

just to point out - according to IDLE, this line is invalid:
# close all threads

Ln: 54, Col: 8

If you could give the full error, that would be better. Also, are you using Python 3.x or 2.x or are you using the EXE?

Code:

#!/usr/bin/env python

"""
This is a Mesh Server for Scratch (and possibly any project)
that you can run in the background on a VPS of the sorts.

It's totally insecure and anyone can connect and attempt to
send messages (whether broadcasts or sensor-updates) to the
users connected.

This version supports Windows! :D
"""

import select
import socket
import sys
import threading

class Server:
    def __init__(self):
        self.host = ''
        self.port = 42001
        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()
        running = 1
        while running:
            
            try:
                c = Client(self.server.accept())
                c.start()
                self.threads.append(c)
            
            except KeyboardInterrupt:
                junk = sys.stdin.readline()
                running = 0

        # 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

    def run(self):
        running = 1
        while running:
            try:
                data = self.client.recv(self.size)
            except Exception, e:
                self.log(str(e))
                data = ''
            
            if data:
                for user in s.threads:
                    if user == self:
                        continue
                    
                    try:
                        user.client.send(data)
                    except Exception, e:
                        self.log(str(e))
            
            else:
                self.client.close()
                running = 0
    
    def log(self, string):
        try:
            log_file = open("mesh.log", 'a')
            log_file.write('\n' + string)
            log_file.close()
        except IOError, e:
            log_file = open("mesh.log", 'w')
            log_file.write(string)
            log_file.close()

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

I'm using the latest Python, and it gives me no error details, it just colours the line red. Also, according to the IDLE help, it says Python version is 2.7.3 - and the same for IDLE.

Last edited by DigiTechs (2012-04-29 03:45:16)


I'm back.
Maybe.

Offline

 

#33 2012-04-29 06:33:21

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

DigiTechs wrote:

just to point out - according to IDLE, this line is invalid:
# close all threads

DigiTechs wrote:

I'm using the latest Python, and it gives me no error details, it just colours the line red. Also, according to the IDLE help, it says Python version is 2.7.3 - and the same for IDLE.

I thought IDLE coloured comments red...  tongue


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

Offline

 

#34 2012-04-30 08:57:25

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

DigiTechs wrote:

Magnie wrote:

DigiTechs wrote:

just to point out - according to IDLE, this line is invalid:
# close all threads

Ln: 54, Col: 8

If you could give the full error, that would be better. Also, are you using Python 3.x or 2.x or are you using the EXE?

Code:

#!/usr/bin/env python

"""
This is a Mesh Server for Scratch (and possibly any project)
that you can run in the background on a VPS of the sorts.

It's totally insecure and anyone can connect and attempt to
send messages (whether broadcasts or sensor-updates) to the
users connected.

This version supports Windows! :D
"""

import select
import socket
import sys
import threading

class Server:
    def __init__(self):
        self.host = ''
        self.port = 42001
        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()
        running = 1
        while running:
            
            try:
                c = Client(self.server.accept())
                c.start()
                self.threads.append(c)
            
            except KeyboardInterrupt:
                junk = sys.stdin.readline()
                running = 0

        # 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

    def run(self):
        running = 1
        while running:
            try:
                data = self.client.recv(self.size)
            except Exception, e:
                self.log(str(e))
                data = ''
            
            if data:
                for user in s.threads:
                    if user == self:
                        continue
                    
                    try:
                        user.client.send(data)
                    except Exception, e:
                        self.log(str(e))
            
            else:
                self.client.close()
                running = 0
    
    def log(self, string):
        try:
            log_file = open("mesh.log", 'a')
            log_file.write('\n' + string)
            log_file.close()
        except IOError, e:
            log_file = open("mesh.log", 'w')
            log_file.write(string)
            log_file.close()

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

I'm using the latest Python, and it gives me no error details, it just colours the line red. Also, according to the IDLE help, it says Python version is 2.7.3 - and the same for IDLE.

I have no idea how you are getting any errors. Can you tell me exactly what you are doing? Opening IDLE > Opening Mesh Server within IDLE > Running Mesh Server is what I'm doing.

Offline

 

#35 2012-05-02 15:06:57

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Magnie wrote:

DigiTechs wrote:

Magnie wrote:


If you could give the full error, that would be better. Also, are you using Python 3.x or 2.x or are you using the EXE?

Code:

#!/usr/bin/env python

"""
This is a Mesh Server for Scratch (and possibly any project)
that you can run in the background on a VPS of the sorts.

It's totally insecure and anyone can connect and attempt to
send messages (whether broadcasts or sensor-updates) to the
users connected.

This version supports Windows! :D
"""

import select
import socket
import sys
import threading

class Server:
    def __init__(self):
        self.host = ''
        self.port = 42001
        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()
        running = 1
        while running:
            
            try:
                c = Client(self.server.accept())
                c.start()
                self.threads.append(c)
            
            except KeyboardInterrupt:
                junk = sys.stdin.readline()
                running = 0

        # 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

    def run(self):
        running = 1
        while running:
            try:
                data = self.client.recv(self.size)
            except Exception, e:
                self.log(str(e))
                data = ''
            
            if data:
                for user in s.threads:
                    if user == self:
                        continue
                    
                    try:
                        user.client.send(data)
                    except Exception, e:
                        self.log(str(e))
            
            else:
                self.client.close()
                running = 0
    
    def log(self, string):
        try:
            log_file = open("mesh.log", 'a')
            log_file.write('\n' + string)
            log_file.close()
        except IOError, e:
            log_file = open("mesh.log", 'w')
            log_file.write(string)
            log_file.close()

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

I'm using the latest Python, and it gives me no error details, it just colours the line red. Also, according to the IDLE help, it says Python version is 2.7.3 - and the same for IDLE.

I have no idea how you are getting any errors. Can you tell me exactly what you are doing? Opening IDLE > Opening Mesh Server within IDLE > Running Mesh Server is what I'm doing.

I'm not running it yet, but just according to what the default IDLE settings are, that line is an error.  tongue


I'm back.
Maybe.

Offline

 

#36 2012-08-07 07:45:41

DigiTechs
Scratcher
Registered: 2011-04-30
Posts: 500+

Re: Mesh Python Server - Host a Mesh Server on a VPS!

Uhmm, I just ran the server through IDLE and this came up:


Traceback (most recent call last):
  File "C:/Users/-myname-/Testserver", line 108, in <module>
    s.run()
  File "C:/Users/-myname-/Testserver", line 48, in run
    [])
TypeError: argument must be an int, or have a fileno() method


I'm back.
Maybe.

Offline

 

#37 2012-08-07 11:41:10

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

Re: Mesh Python Server - Host a Mesh Server on a VPS!

DigiTechs wrote:

Uhmm, I just ran the server through IDLE and this came up:


Traceback (most recent call last):
  File "C:/Users/-myname-/Testserver", line 108, in <module>
    s.run()
  File "C:/Users/-myname-/Testserver", line 48, in run
    [])
TypeError: argument must be an int, or have a fileno() method

The server is designed for Linux systems, not Windows. Windows doesn't support the select module.  sad

Offline

 

Board footer