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.
Offline
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?
#!/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
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?
Offline
Could you explain more about how this can be configured?
I'm kinda new to python.
Offline
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.
Offline
Magnie wrote:
DigiTechs wrote:
just to point out - according to IDLE, this line is invalid:
# close all threads
Ln: 54, Col: 8If 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)
Offline
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...
Offline
DigiTechs wrote:
Magnie wrote:
DigiTechs wrote:
just to point out - according to IDLE, this line is invalid:
# close all threads
Ln: 54, Col: 8If 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
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.
Offline
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
Offline
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.
Offline