class ServerSCTP_One2Many: server = sctp.sctpsocket(socket.AF_INET, sctp.UDP_STYLE, 0) def __init__(self, bind_ip, bind_port): serv_add = (bind_ip, bind_port) server = ServerSCTP_One2Many.server server.bind(serv_add) server.listen(5) print("[*] listening on {}:{}".format(bind_ip, bind_port)) def connect(self): signal.signal(signal.SIGCHLD, signal.SIG_IGN) server = ServerSCTP_One2Many.server while True: fromaddr, flags, data, notif = server.sctp_recv(buffer_size) if len(data) > 0: #if client pass sth if data[:2].decode( "utf-8" ) == 'cd': #cd doesen't work in subprocess.Popen try: #try-except prevents executing incorrect command os.chdir(data[3:].decode("utf-8")) cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output_bytes = cmd.stdout.read() output_str = str(output_bytes, "utf-8") server.sctp_send( str.encode(output_str + str(os.getcwd()) + '$'), fromaddr) except: server.sctp_send( str.encode('Wrong folder!\n' + str(os.getcwd()) + '$'), fromaddr) else: #executing command and sending results cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output_bytes = cmd.stdout.read() output_str = str(output_bytes, "utf-8") server.sctp_send( str.encode(output_str + str(os.getcwd()) + '$'), fromaddr) def close(self): server = ServerSCTP_One2Many.server server.close()
import socket import os import subprocess import signal import sys import threading import sctp bind_ip = '127.0.0.1' bind_port = 8880 serv_add = (bind_ip, bind_port) server = sctp.sctpsocket(socket.AF_INET, sctp.UDP_STYLE, 0) server.bind(serv_add) server.listen(5) print("[*] listening on {}:{}".format(bind_ip, bind_port)) while True: fromaddr, flags, data, notif = server.sctp_recv(1024) if data[:2].decode("utf-8") == 'cd': os.chdir(data[3:].decode("utf-8")) if len(data) > 0: cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output_bytes = cmd.stdout.read() output_str = str(output_bytes, "utf-8")
class ServerSCTP_One2One: server = sctp.sctpsocket(socket.AF_INET, sctp.TCP_STYLE, 0) def __init__(self, bind_ip, bind_port): serv_add = (bind_ip, bind_port) server = ServerSCTP_One2One.server server.bind((serv_add)) server.listen(5) print("[*] listening on {}:{}".format(bind_ip, bind_port)) def connect(self): signal.signal(signal.SIGCHLD, signal.SIG_IGN) server = ServerSCTP_One2One.server while True: conn, addr = server.accept() print('Accepted connection from {} and port {}'.format( addr[0], addr[1])) process_id = os.fork() if process_id == 0: server.close() while True: prctl.set_pdeathsig( signal.SIGKILL ) #send SIGKILL to child if parent was killed (this is the signal that the calling process will get when its parent die) fromaddr, flags, data, notif = conn.sctp_recv(buffer_size) if len(data) > 0 and data[:4].decode( "utf-8") != 'quit': #if client pass sth if data[:2].decode( "utf-8" ) == 'cd': #cd doesen't work in subprocess.Popen try: #try-except prevents executing incorrect command os.chdir(data[3:].decode("utf-8")) cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output_bytes = cmd.stdout.read() output_str = str(output_bytes, "utf-8") conn.sctp_send( str.encode(output_str + str(os.getcwd()) + '$')) except: conn.sctp_send( str.encode('Wrong folder!\n' + str(os.getcwd()) + '$')) else: #executing command and sending results cmd = subprocess.Popen(data[:], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) output_bytes = cmd.stdout.read() output_str = str(output_bytes, "utf-8") conn.sctp_send( str.encode(output_str + str(os.getcwd()) + '$')) if data[:4].decode( "utf-8" ) == 'quit': #if client send "quit" conection will end child_pid = os.getpid() print('Child procees with PID {} has been terminated'. format(child_pid)) os.kill(child_pid, signal.SIGKILL) break if process_id > 0: conn.close() conn.close() def close(self): server = ServerSCTP_One2One.server server.close()
import json import socket import sctp sock = sctp.sctpsocket(socket.AF_INET, sctp.TCP_STYLE, None) sock.connect(('127.0.0.1', 9999)) for i in range(5): sock.sctp_send(json.dumps({'type': 'send', 'user': '******', 'msg': i}))
def __init__(self): self.__sock = sctp.sctpsocket(socket.AF_INET, sctp.TCP_STYLE, None) self.__sock.setblocking(True) self.log = []
# # You should have received a copy of the GNU Lesser General Public License # along with this library; If not, see <http://www.gnu.org/licenses/>. ''' This is simple test with sctpsocket() on Python3. The test procedure is: 1) sctp_test -H 127.0.0.1 -P 10000 -l 2) use python3 to run this script 3) verify if sctp_test receives "hello" ''' import sctp server_addr = ("127.0.0.1", 10000) print("SCTP server:", server_addr) sk = sctp.sctpsocket(sctp.socket.AF_INET, sctp.socket.SOCK_STREAM, None) sk.initparams.max_instreams = 3 sk.initparams.num_ostreams = 3 sk.events.clear() sk.events.data_io = 1 sk.connect(server_addr) msg = "hello" sk.sctp_send(msg) print("Sent:", msg) #fromaddr, flags, msg, notif = sk.sctp_recv(1024) #print("Received:", msg) sk.close()