我有一个Python程序,客户端将文件上传到服务器,然后服务器将文件发送回客户端,或者客户端从服务器下载文件。我希望最终在用户定义的网络中使用Docker实现这一点
因此,我将客户机和服务器分别作为容器进行了容器化。出现的问题是,当我为它们执行docker run
时,它没有显示任何结果或错误,它只是在命令行上,就像被卡住了一样,或者处于无限循环中。我将附加我正在使用/执行的代码和docker run
命令
如何调试此问题
注意:这不是我的Python程序,我也不打算让它成为我的。我在互联网的某个公共论坛上找到了这段代码。我只是用它来测试Docker(我是Docker的初学者)
client.py和Dockerfile的代码
import socket
csFT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
csFT.connect(("server", 8756))
text_file = "/client/send.txt"
#Send file
with open(text_file, 'rb') as fs:
#Using with, no file close is necessary,
#with automatically handles file close
csFT.send(b'BEGIN')
while True:
data = fs.read(1024)
print('Sending data', data.decode('utf-8'))
csFT.send(data)
print('Sent data', data.decode('utf-8'))
if not data:
print('Breaking from sending data')
break
csFT.send(b'ENDED')
fs.close()
#Receive file
print("Receiving..")
with open(text_file, 'wb') as fw:
while True:
data = csFT.recv(1024)
if not data:
break
fw.write(data)
fw.close()
print("Received..")
csFT.close()
Dockerfile
FROM python:alpine3.10
COPY client.py /client/
COPY send.txt /client/
WORKDIR /client/
CMD python ./client.py
server.py和dockerfile的代码
import socket
ssFT = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssFT.bind((socket.gethostname(), 8756))
ssFT.listen(1)
while True:
(conn, address) = ssFT.accept()
text_file = "/server/fileProj.txt"
#Receive, output and save file
with open(text_file, "wb") as fw:
print("Receiving..")
while True:
print('receiving')
data = conn.recv(32)
if data == b'BEGIN':
continue
elif data == b'ENDED':
print('Breaking from file write')
break
else:
print('Received: ', data.decode('utf-8'))
fw.write(data)
print('Wrote to file', data.decode('utf-8'))
fw.close()
print("Received..")
#Append and send file
#print('Opening file ', text_file)
with open(text_file, 'ab+') as fa:
print('Opened file')
print("Appending string to file.")
string = b"Append this to file."
fa.write(string)
fa.seek(0, 0)
print("Sending file.")
while True:
data = fa.read(1024)
conn.send(data)
if not data:
break
fa.close()
print("Sent file.")
break
ssFT.close()
Dockerfile
FROM python:alpine3.10
WORKDIR /server/
EXPOSE 8756
COPY server.py /server/
COPY fileProj.txt /server/
CMD python ./server.py
我在命令行上执行的命令是
docker network create mynet
docker run --name server -p 8756:8756 --network mynet server:1.0
docker run --name client --network mynet client:1.0
目前没有回答
相关问题 更多 >
编程相关推荐