Я новичок Python, и я пишу эту удаленную оболочку на основе центрального сервера и двух клиентов (один отправляет команды, а другой получает их и отправляет вывод).
ОС Является ли Windows 10 64-битным, а Python - 3,7.
Если я посылаю строки или простые переменные из одной в другую, это нормально, но когда, например, я хочу отправить вывод file.read () или подпроцесса, я был отключен от центрального сервера с:
Вы были отключены от сервера
Что мне здесь не хватает?
Код сервера:
import socket
import threading
#Variables for holding information about connections
connections = []
total_connections = 0
#Client class, new instance created for each connected client
#Each instance has the socket and address that is associated with items
#Along with an assigned ID and a name chosen by the client
class Client(threading.Thread):
def __init__(self, socket, address, id, name, signal):
threading.Thread.__init__(self)
self.socket = socket
self.address = address
self.id = id
self.name = name
self.signal = signal
def __str__(self):
return str(self.id) + " " + str(self.address)
#Attempt to get data from client
#If unable to, assume client has disconnected and remove him from server data
#If able to and we get data back, print it in the server and send it back to every
#client aside from the client that has sent it
#.decode is used to convert the byte data into a printable string
def run(self):
while self.signal:
try:
data = self.socket.recv(1024)
except:
print("Client " + str(self.address) + " has disconnected")
self.signal = False
connections.remove(self)
break
if data != "":
print("ID " + str(self.id) + ": " + str(data.decode("utf-8")))
for client in connections:
if client.id != self.id:
client.socket.sendall(data)
#Wait for new connections
def newConnections(socket):
while True:
sock, address = socket.accept()
global total_connections
connections.append(Client(sock, address, total_connections, "Name", True))
connections[len(connections) - 1].start()
print("New connection at ID " + str(connections[len(connections) - 1]))
total_connections += 1
def main():
#Get host and port
host = "192.168.1.240"
port = 9876
#Create new server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(5)
#Create new thread to wait for connections
newConnectionsThread = threading.Thread(target = newConnections, args = (sock,))
newConnectionsThread.start()
main()
Код клиента, который выполняет команды и который отключается есть комментарий, где он отключается, он делает то же самое с выводом file.read ()
import socket
import threading
import sys
import requests
import subprocess
import time
hisID = "Server:"
def telegram_bot_sendtext(bot_message):
bot_token = 'xxxxxx'
bot_chatID = 'xxxxxx'
send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response = requests.get(send_text)
return response.json()
#Wait for incoming data from server
#.decode is used to turn the message in bytes to a string
def receive(socket, signal):
while signal:
try:
data = socket.recv(1024)
command = str(data.decode("utf-8"))
print(command)
if command == "oof":
sock.sendall(str.encode("YOU TOLD ME TO oof?"))
telegram_bot_sendtext("oof")
elif command == "command":
out = subprocess.check_output("dir", shell=True)
sock.sendall(str.encode(out)) #HERE I GOT DISCONNECTED
else:
sock.sendall(str.encode("Command not recognized"))
except:
print("You have been disconnected from the server")
signal = False
break
#Get host and port
host = "192.168.1.240"
port = 9876
#Attempt connection to server
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
except:
print("Could not make a connection to the server")
input("Press enter to quit")
sys.exit(0)
#Create new thread to wait for data
receiveThread = threading.Thread(target = receive, args = (sock, True))
receiveThread.start()
#Send data to server
#str.encode is used to turn the string message into bytes so it can be sent across the network
while True:
try:
input()
except (KeyboardInterrupt, SystemExit):
sock.close()
sys.exit()
Код клиента, который отправляет команды
import socket
import threading
import sys
hisID = "Client:"
#Wait for incoming data from server
#.decode is used to turn the message in bytes to a string
def receive(socket, signal):
while signal:
try:
data = socket.recv(1024)
command = str(data.decode("utf-8"))
print(hisID + ">" + command )
except:
print("You have been disconnected from the server")
signal = False
break
#Get host and port
host = "192.168.1.240"
port = 9876
#Attempt connection to server
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
except:
print("Could not make a connection to the server")
input("Press enter to quit")
sys.exit(0)
#Create new thread to wait for data
receiveThread = threading.Thread(target = receive, args = (sock, True))
receiveThread.start()
#Send data to server
#str.encode is used to turn the string message into bytes so it can be sent across the network
while True:
message = input(":>")
sock.sendall(str.encode(message))