|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
import hashlib
|
|
|
|
|
import json
|
|
|
|
|
import queue
|
|
|
|
|
import selectors
|
|
|
|
|
import socket
|
|
|
|
|
import threading
|
|
|
|
@ -36,36 +37,54 @@ class recver_net():
|
|
|
|
|
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
|
self.server.bind(('0.0.0.0', self.port))
|
|
|
|
|
self.server.listen(10)
|
|
|
|
|
|
|
|
|
|
self.result_queue = queue.Queue()
|
|
|
|
|
self.FLAG = 0
|
|
|
|
|
def recver_thread(self):
|
|
|
|
|
while True:
|
|
|
|
|
conn, addr = self.server.accept()
|
|
|
|
|
data = conn.recv(10240)
|
|
|
|
|
msg = json.loads(data.decode())
|
|
|
|
|
if int(msg['flag']) == 0:
|
|
|
|
|
print("[+] haved received data:" + msg["data"])
|
|
|
|
|
elif int(msg['flag']) == 1:
|
|
|
|
|
self.recv_file(msg['data'])
|
|
|
|
|
if self.FLAG == 0:
|
|
|
|
|
try:
|
|
|
|
|
conn, addr = self.server.accept()
|
|
|
|
|
data = conn.recv(10240)
|
|
|
|
|
msg = json.loads(data.decode())
|
|
|
|
|
if int(msg['flag']) == 0:
|
|
|
|
|
print("[+] haved received data:" + msg["data"])
|
|
|
|
|
elif int(msg['flag']) == 1:
|
|
|
|
|
self.recv_file(msg['data'])
|
|
|
|
|
else:
|
|
|
|
|
print("[x] Error")
|
|
|
|
|
except :
|
|
|
|
|
print("[x] Error")
|
|
|
|
|
self.FLAG = 1
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
print("[x] Error")
|
|
|
|
|
print("[x] loss connection with client,exiting...")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def send_thread(self, conn):
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
flag: int = int(input())
|
|
|
|
|
if flag == 0:
|
|
|
|
|
data = input("[-] Enter data to send: ")
|
|
|
|
|
msg = {"flag": 0, "data": data}
|
|
|
|
|
msg = json.dumps(msg).encode("utf-8")
|
|
|
|
|
self.send_data(conn, msg)
|
|
|
|
|
elif flag == 1:
|
|
|
|
|
print("[+] Calling file transfer module...")
|
|
|
|
|
# file_name = input("Enter file name to send: ")
|
|
|
|
|
self.send_file(conn)
|
|
|
|
|
else:
|
|
|
|
|
print("[x] Error,plesae enter 0 to send data, or 1 to send file.")
|
|
|
|
|
except ValueError:
|
|
|
|
|
print("[x] Error, please enter a valid number.")
|
|
|
|
|
if self.FLAG == 0:
|
|
|
|
|
try:
|
|
|
|
|
flag = input()
|
|
|
|
|
if flag == "0":
|
|
|
|
|
data = input("[-] Enter data to send: ")
|
|
|
|
|
msg = {"flag": 0, "data": data}
|
|
|
|
|
msg = json.dumps(msg).encode("utf-8")
|
|
|
|
|
self.send_data(conn, msg)
|
|
|
|
|
elif flag == "1":
|
|
|
|
|
print("[+] Calling file transfer module...")
|
|
|
|
|
# file_name = input("Enter file name to send: ")
|
|
|
|
|
self.send_file(conn)
|
|
|
|
|
else:
|
|
|
|
|
print("[x] Error,plesae enter 0 to send data, or 1 to send file.")
|
|
|
|
|
except:
|
|
|
|
|
print("[x] Error,远程主机已将连接断开。正在尝试重连...")
|
|
|
|
|
self.FLAG = 1
|
|
|
|
|
return
|
|
|
|
|
else:
|
|
|
|
|
print("[x] lose connection with client,exiting...")
|
|
|
|
|
conn.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def send_data(self, conn, data):
|
|
|
|
|
conn.sendall(data)
|
|
|
|
@ -89,8 +108,7 @@ class recver_net():
|
|
|
|
|
letter: Letter = json_to_obj(data)
|
|
|
|
|
recv.handleLetter(letter)
|
|
|
|
|
|
|
|
|
|
def main(self):
|
|
|
|
|
|
|
|
|
|
def connect(self):
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
client = socket.socket() # 定义协议类型,相当于生命socket类型,同时生成socket连接对象
|
|
|
|
@ -101,9 +119,35 @@ class recver_net():
|
|
|
|
|
break
|
|
|
|
|
except socket.error:
|
|
|
|
|
print("[*] Waiting for ...")
|
|
|
|
|
# 加入线程
|
|
|
|
|
threading.Thread(target=self.recver_thread, ).start()
|
|
|
|
|
threading.Thread(target=self.send_thread, args=(client,)).start()
|
|
|
|
|
return client
|
|
|
|
|
def main(self):
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
if self.FLAG == 0:
|
|
|
|
|
client = self.connect()
|
|
|
|
|
a = threading.Thread(target=self.recver_thread, )
|
|
|
|
|
a.start()
|
|
|
|
|
b=threading.Thread(target=self.send_thread, args=(client,))
|
|
|
|
|
b.start()
|
|
|
|
|
b.join()
|
|
|
|
|
elif self.FLAG == 1:
|
|
|
|
|
self.FLAG = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# # 加入线程
|
|
|
|
|
# if self.FLAG == 0:
|
|
|
|
|
# a=threading.Thread(target=self.recver_thread, )
|
|
|
|
|
# b=threading.Thread(target=self.send_thread, args=(client,))
|
|
|
|
|
# a.start()
|
|
|
|
|
# b.start()
|
|
|
|
|
# elif self.FLAG == 1:
|
|
|
|
|
# # 主动杀死线程a和b,且不等待完成直接杀死
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
threading.Thread(target=self.main).start()
|
|
|
|
|