[英]Can't connect client to docker python server
我正在尝试连接在 docker (compose) 下运行的 python 服务器并在 Ubuntu 上收到“连接被拒绝”错误。 任何帮助表示赞赏。
使用 docker compose 构建我的服务器的命令:
sudo docker-compose up -d
客户端尝试使用 curl 进行连接:(192.168.1.149 是 Ubuntu 机器的静态 IP),我尝试使用 127.0.0.1 和 docker 映像的 IP 地址以及 0.0.0.0 没有任何区别。
curl --data "Testing" 192.168.1.149:5149
curl 命令的响应:
curl: (7) Failed to connect to 192.168.1.149 port 5149: Connection refused
dockerfile_test:
FROM python:3.8
EXPOSE 5149
COPY ./test_main.py /
CMD ["python", "-u", "./test_main.py"]
docker-compose.yaml:
version: '3'
services:
test_expose:
build:
context: /home/home/mydockers/custom_docker/test_expose_port
dockerfile: dockerfile_test
image: test_expose
container_name: test_expose
ports:
- 5149:5149
restart: unless-stopped
test_main.py:
import socket
import time
socket_receive_buffer = 4096
def main():
try:
print("Create socket")
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print("Bind socket")
sock.bind(('0.0.0.0', 5149))
while True:
print("In loop waiting for data")
data, address = sock.recvfrom(socket_receive_buffer)
print("received data")
string_data = data.decode('utf-8')
print(string_data)
except Exception as e:
print("got error")
if sock is not None:
sock.close()
print("Exception " + str(e))
time.sleep(60)
main()
您正在运行 UDP 服务器,因此:
curl
正在发送一个 HTTP 请求,该请求通过 TCP 运行。 因此curl
将永远无法与您的服务器通信。除了需要解决的这两个问题之外,我还建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.