ich habe in zwei skripten eine "socketverwaltung" geschrieben, und dachte mir, dass sich da soviel wiederholt, dass ich ein codesnippet mache. ob ihr es brauchen könnt, ka, aber ich
Code: Alles auswählen
#networker.py
#stdlib
import socket
import time
import sys
#own lib
import firewall
class MainSocket:
__doc__ = """this the mainsocket where you can read and write to"""
def __init__(self, mainport):
self.closed = False
recvport = mainport + 1
self.sendport = mainport
self.sendsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.recvsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.recvsocket.settimeout(5)
self.recvsocket.bind(('localhost', recvport))
self.my_firewall = firewall.FireWall()
def search(self, seconds):
endtime = time.time() + seconds
self.recvsocket.settimeout(1)
addrs = list()
verifications = list()
while endtime > time.time():
try:
self.recvsocket.listen(1)
conn, (addr, strange_number) = self.recvsocket.accept()
addrs.append(addr)
verification = conn.recv(1024)
verifications.append(verification)
print "%s tried to connect using %s" % (addr, verification)
except socket.timeout:
pass
for addr in addrs:
if addr in self.my_firewall.get_addrs():
continue
self.recvsocket.settimeout(5)
print (addrs, verifications)
return (addrs, verifications)
# def read(self, bufsize=1024):
# __doc__ = "'reads' from the socket. recieves data from the recvsocket"
# self.recvsocket.listen(5)
# conn, addr = self.recvsocket.accept()
# if addr in self.my_firewall.get_addrs():
# return None
# return conn.recv(bufsize)
def write(self, data, address):
__doc__ = """'writes' data to the socket.
sends the data to the given address.
the connection i use must be got by search()!"""
self.sendsocket.connect((address, self.sendport))
self.sendsocket.send(data)
self.sendsocket.close()
def close(self):
self.sendsocket.close()
self.recvsocket.close()
self.closed = True
def __del__(self):
if not self.closed:
self.close()
class SocketPair:
__doc__ = """this is a socketpair where you can read and write to"""
def __init__(self, ip_address, sendport, recvport, is_server):
self.closed = False
self.sendsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tmp_recvsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if is_server:
print "creating the reciever..."
tmp_recvsocket.bind(('localhost', recvport))
tmp_recvsocket.listen(1)
self.recvsocket, addr = tmp_recvsocket.accept()
print "creating the sender..."
for i in range(20):
try:
self.sendsocket.connect((ip_address, sendport))
except socket.error:
continue
time.sleep(1)
else:
print "creating the sender..."
self.sendsocket.connect((ip_address, sendport))
print "creating the reciever..."
tmp_recvsocket.bind(('localhost', recvport))
tmp_recvsocket.listen(1)
self.recvsocket, addr = tmp_recvsocket.accept()
tmp_recvsocket.close()
def read(self, bufsize=1024):
__doc__ = "'reads' from the socket. recieves data from the recvsocket"
return self.recvsocket.recv(bufsize)
def write(self, data):
__doc__ = """'writes' data to the socket.
sends the data to the given address"""
self.sendsocket.sendall(data)
def close(self):
self.sendsocket.close()
self.recvsocket.close()
self.closed = True
def __del__(self):
if not self.closed:
self.close()
class ServerSockets:
__doc__ = """this contents all the sockets of the gamers.
the mainport is the startpoint of naming the sockets.
so if the mainsocket is 21000, the first socketpair will be 21001
and 21002, the second 21003 and 21004 and so on."""
def __init__(self, mainport):
self.maincon = MainSocket(mainport)
self.sockets = dict()
self.ports = [mainport, mainport+1]
def search(self, seconds):
packets = self.maincon.search(seconds)
addrs, verifications = packets
for packet in zip(addrs, verifications):
print 'a packet looks like ', packet
addr, verification = packet
print "%s tried to connect to localhost using '%s'" % (addr,
verification)
return packets
def create(self, name, ip_address):
__doc__ = "creates a socket from some standard-parametres"
sendport = self.ports[-1] + 1
self.ports.append(sendport)
recvport = sendport + 1
print "i send to %i and recieve on %i" % (sendport, recvport)
self.ports.append(recvport)
print "values...", str(sendport), ip_address
self.maincon.write(str(sendport), ip_address)
print "client recieved its port..."
self.sockets[name] = SocketPair(ip_address, sendport, recvport, True)
print "the socket for the server is made..."
print "the client '%s' is created!" % (name, )
return self.sockets[name]
def connection(self, name):
__doc__ = """
returns the real connection which can be used to talk to a client"""
return self.sockets[name]
def close(self, name):
__doc__ = "closes one socket where the owner has the name 'name'"
self.sockets[name].close()
del self.sockets[name]
def close_all(self):
__doc__ = "closes all sockets"
for _socket in self.sockets.values():
_socket.close()
#now this is for the client
def initiate_connection(host, mainport, verification):
sendport = mainport + 1
recvport = mainport
print "i send to %i and recieve on %i" % (sendport, recvport)
sendsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tmp_recvsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tmp_recvsocket.bind(('localhost', recvport))
print "now connecting to the host..."
sendsocket.connect((host, sendport))
sendsocket.send(verification)
tmp_recvsocket.listen(1)
recvsocket, addr = tmp_recvsocket.accept()
answer = recvsocket.recv(1024)
print answer
new_port = int(answer)
print "the new recv-port is %i, so the sendport is %i" % (new_port, new_port+1)
tmp_recvsocket.close()
recvsocket.close()
return new_port
def connect(host, port, verification):
__doc__ = "the client which can access on this system"
recvport = initiate_connection(host, port, verification)
sendport = recvport + 1
print "i send to %i and recieve on %i" % (sendport, recvport)
return SocketPair(host, sendport, recvport, False)
Code: Alles auswählen
#firewall
#a little 'firewall' saving unallowed ip-addresses.
#there is a log-file where every attack is written.
#if it is 24:00:00 or later, the file will be removed.
import os, time
class FireWall:
def __init__(self):
return None
if not os.path.exists('logfile.log'):
self.log_file = file('logfile.log', 'w+')
last_mod = os.path.getmtime('logfile.log')
mod_day = time.strftime('%j', time.gmtime(last_mod))
mod_year = time.strftime('%Y', time.gmtime(last_mod))
actual_day = time.strftime('%j')
actual_year = time.time.strftime('%Y')
if mod_year < actual_year or mod_day < actual_day:
os.remove('logfile.log')
self.log_file = file('logfile.log', 'w+')
def add_addr(self, addr):
#self.log_file.write('%s\n' % addr)
pass
def get_addrs(self):
#return self.log_file.readlines()
return []
def shutdown(self):
pass
#self.log_file.close()