【发布时间】:2018-07-13 14:26:02
【问题描述】:
我正在阅读 > 并尝试第一个 bhnet.py 程序。
在一个终端上,我运行脚本
./bhnet.py -l -p 9999 -c
在另一个终端上,运行脚本
./bhnet.py -t localhost -p 9999
然后输入
<ctrl-D>
或
ls -alt
<ctrl-D>
第一个终端会返回
File "bhnet.py", line 186, in client_handler
cmd_buffer += client_socket.recv(1024).decode('utf-8')
ConnectionResetError: [Errno 54] Connection reset by peer
以下是程序的代码
def client_handler(client_socket):
global upload
global execute
global command
# check for upload
if len(upload_destination):
# read all the buffer and write to destination
file_buffer = ""
# keep reading til none is available
while True:
data = client_socket.recv(1024)
if not data:
break
else:
file_buffer += data
# take the bytes and write them out
try:
file_descriptor = open(upload_destination,'wb')
file_descriptor.write(file_buffer)
file_descriptor.close()
# acknowledge that file being wrote out
client_socket.send(f"Successfully save file to {upload_destination}.\r \n")
except:
client_socket.send(f"Failed to save file to {upload_destination}.\r \n")
# check for command execution
if command:
while True:
#pop up a window
client_socket.send(b"<BHP:#> ")
# keep receiving data until \n
cmd_buffer = ""
while "\n" not in cmd_buffer:
cmd_buffer += client_socket.recv(1024).decode('utf-8')
response = run_command(cmd_buffer)
client_socket.send(response)
我用谷歌搜索,甚至尝试升级 openssl,但这些都不起作用...
提前致谢!
【问题讨论】:
标签: python-3.x sockets