Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import socket
- import threading
- import random
- thread_limit = 100
- thread_start = 0
- thread_id = []
- syntax = '!'
- def get_thread_id():
- tid = random.randint(1, 100)
- thread_id.append(tid)
- return tid
- def client(conn, addr, tid):
- global thread_start
- global thread_limit
- global thread_id
- global syntax
- try:
- conn.send("[server] session assigned with thread id %s\r\n" % (tid))
- conn.send("cmd> ")
- cmd = conn.recv(512)
- if (len(cmd) > 512):
- conn.close()
- print("[client] client sent a string longer than recv limit")
- thread_start -= 1
- ncmd = cmd.split(' ')
- if (ncmd[0] == syntax+"logout"):
- conn.close()
- print("[client] executed logout. ending thread %d" % (tid))
- elif (ncmd[0] == syntax+"threads"):
- conn.send("Total Threads [%d]" % (len(thread_id)))
- except Exception as e:
- print(str(e))
- print("[client] client ended session [thread id %s]" % (tid))
- thread_start -= 1
- if(conn):
- conn.close()
- def server():
- global thread_start
- global thread_limit
- global thread_id
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- try:
- sock.bind(("0.0.0.0", 701))
- except socket.error:
- print("[-] Socket Error: Could not bind address. Address already in use.")
- sys.exit()
- sock.listen(0)
- try:
- while True:
- conn, addr = sock.accept()
- print("[server] connection recieved from [%s:%s]" % (addr[0], addr[1]))
- if (thread_limit == thread_start):
- print("[server] could not send client to shell [Maximum threads used]")
- elif (thread_limit != thread_start):
- print("[server] sent client to shell.")
- tid = get_thread_id()
- print("[server] thread started | ID %d" % (tid))
- thread_begin = threading.Thread(target=client, args=(conn, addr, tid,)).start()
- thread_start += 1
- except socket.error:
- print("[-] You wont ever get this message. just saying.")
- server()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement