I don’t really think that would be a good idea. Like Moguri said file IO tends to be expensive. If you wanted to do something like that, I would suggest to do it with Python. I actually made a module just for doing that.
It’s pretty easy to use and I filled it with doc strings. I doubt I’m going to be adding much else to it, so feel free to use it. Also, I made a thread with a short example in it, you can try look at it.
Example:
http://blenderartists.org/forum/showthread.php?t=206115
Module: bi_net.py
import socket
"""A module for performing socket input and output with less work.
This module offers a simple way of using a UDP socket. There
is only support for IP, and the classes are best used as
superclasses.
"""
class Server:
"""Class for receiving input and output from multiple clients.
The constructor takes no arguments. Instead is checks if IP,
PORT, BUFF_SIZE, or TIMEOUT have been defined in self, if not, they are
given default values.
"""
def __init__(self):
"""Constructor. See class doc string."""
if not ('IP' in self.__dict__):
self.IP = socket.gethostbyname(socket.gethostname())
if not ('PORT' in self.__dict__):
self.PORT = 21500
if not ('BUFF_SIZE' in self.__dict__):
self.BUFF_SIZE = 1024
if not ('TIMEOUT' in self.__dict__):
self.TIMEOUT = 0.005
self.server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.server.bind((self.IP, self.PORT))
self.server.settimeout(self.TIMEOUT)
self.clients = []
def recv(self):
"""Returns all packets that have been received.
Each time a packet is succesfully received, the on_input
method is called.
If an error has occured while receiving a packet, it is
assumed that a client has disconnected.
Once a client has disconnected, they are removed from the
self.clients list, and the on_disconnect method is called.
Whenever a new client connects, their address is added to
the self.clients list, and the on_connect method is called.
"""
concatenated = ''
data = bytes()
addr = tuple()
while 1:
try:
data, addr = self.server.recvfrom(self.BUFF_SIZE)
except socket.timeout:
return concatenated
except socket.error:
for i in self.clients:
if not self.connected(i):
self.clients.remove(i)
self.on_disconnect(i)
continue
data = data.decode('utf-8')
if not (addr in self.clients):
self.clients.append(addr)
self.on_connect(addr)
data = self.on_input(data, addr)
concatenated += data
def send(self, data):
"""Sends data to each client.
Every time, before data is sent to a client, the on_output
method is called.
"""
if hasattr(data, 'encode'):
data = data.encode('utf-8')
for client in self.clients:
output = data
output = self.on_output(output, client)
self.server.sendto(output, client)
def connected(self, address):
"""Verifies that an address is opened."""
connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
connection.connect((host, port))
connection.close()
return True
except:
return False
def on_input(self, data, address):
"""Runs when a new packet of data is received."""
return data
def on_output(self, data, address):
"""Runs each time a new packet of data is sent."""
return data
def on_connect(self, address):
"""Runs each time a new client has connected."""
pass
def on_disconnect(self, address):
"""Runs when a client has disconnected."""
pass
class Client:
"""Class for receiving input and output from a server.
The constructor takes no arguments. Instead is checks if IP,
PORT, BUFF_SIZE, or TIMEOUT have been defined in self, if not, they are
given default values.
"""
def __init__(self):
"""Constructor. See class doc string."""
if not ('IP' in self.__dict__):
self.IP = socket.gethostbyname(socket.gethostname())
if not ('PORT' in self.__dict__):
self.PORT = 21500
if not ('BUFF_SIZE' in self.__dict__):
self.BUFF_SIZE = 1024
if not ('TIMEOUT' in self.__dict__):
self.TIMEOUT = 0.005
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.connect((self.IP, self.PORT))
self.socket.settimeout(self.TIMEOUT)
def recv(self):
"""Returns all packets that have been received.
Each time a packet is succesfully received, the on_input
method is called.
If an error has occured while receiving a packet, it is
assumed that a server has disconnected.
Once the server is no longer connected the on_disconnect
method is called.
"""
while 1:
input = ''
data = bytes()
try:
data = self.socket.recv(self.BUFF_SIZE)
except socket.timeout:
return input
except socket.error:
self.socket.close()
self.on_disconnect()
break
data = data.decode('utf-8')
data = self.on_input(data)
input += data
def send(self, data):
"""Sends data to the server."""
if hasattr(data, 'encode'):
data = data.encode('utf-8')
self.socket.send(data)
def on_disconnect(self):
"""Runs once the server is not detected."""
pass
def on_input(self, data):
"""Runs each time a packet of data is received."""
return data