Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pickle
- import socket
- from _thread import start_new_thread
- def log_it(msg):
- global log
- print(msg)
- log.append(msg)
- def generate_reply(msg, conn):
- if msg == hash("file start"):
- attribs = pickle.loads(conn.recv(2048))
- blocks = list()
- for n in range(0, attribs["blocks"]):
- blocks.append(pickle.loads(conn.recv(2048)))
- file = ''.join(blocks)
- try:
- if attribs["encoding"] is not None:
- with open(attribs["name"], 'w' + 'b' if attribs["binary"] else '', encoding=attribs["encoding"]) as f:
- f.write(file)
- else:
- with open(attribs["name"], 'w' + 'b' if attribs["binary"] else '') as f:
- f.write(file)
- except:
- return 0
- return 1
- def threaded_client(conn: socket.socket):
- conn.send(str.encode("Connected"))
- reply = ""
- while True:
- try:
- data = pickle.loads(conn.recv(2048))
- if not data:
- log_it("Disconnected")
- break
- else:
- reply = generate_reply(data, conn)
- log_it("Received: " + data)
- log_it("Sending: " + reply)
- conn.sendall(pickle.dumps(reply, protocol=4))
- except Exception as e:
- raise
- log_it("Lost connection")
- conn.close()
- server = "192.168.0.12"
- # server = ""
- port = 5555
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- log = list()
- try:
- s.bind((server, port))
- except socket.error as e:
- log_it(e)
- s.listen()
- log_it("Server boot successful.")
- if __name__ == "__main__":
- while True:
- conn, addr = s.accept()
- log_it("Connected to: " + str(addr))
- start_new_thread(threaded_client, (conn,))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement