python - Python套接字:可以发送字符串但不能发送“输出”(从套接字断开)
问题描述
我是一名 Python 初学者,我正在基于中央服务器和两个客户端(一个发送命令,另一个接收命令并发送输出)编写这个远程 shell。
操作系统是 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))
解决方案
代码中的裸露except
隐藏了编码错误。理想情况下,只捕获您期望的错误(套接字错误OSError
用作基类),因此不会隐藏意外错误。您也可以将异常原因添加到错误消息中。
except OSError as e:
print(f"You have been disconnected from the server: {e}")
通过此更改,您将清楚地看到问题:
Traceback (most recent call last):
File "C:\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\client.py", line 34, in receive
sock.sendall(str.encode(out)) #HERE I GOT DISCONNECTED
TypeError: descriptor 'encode' requires a 'str' object but received a 'bytes'
错误修复是使用sock.sendall(out)
.
推荐阅读
- java - 为什么Java中的最后一个地图元素不断重复
- python - filenotfound 错误 - chromedriver(网页抓取)
- python - 我们如何在python中使列表中的重复值唯一?
- c# - 如何在没有“if statement”的情况下检查 CheckBox 是否被选中?
- vue.js - 如何用 jest 测试 nuxt?
- coq - 如何使用 VST/coq 为 switch 语句编写证明?
- python - 如何根据另一个列名聚合熊猫中的文本
- reactjs - 如何使用 React 函数组件获取图像显示
- python - Django ValueError:字段 'id' 需要一个数字,但得到了 'myuser'
- python - 查找黑色轮廓之间的区域