简体   繁体   English

如何使用Docker容器内的python套接字连接到服务器?

[英]How can I connect to a server using python sockets inside a Docker container?

I am new to Docker and I am trying to run my python program in a container. 我是Docker的新手,正在尝试在容器中运行python程序。

My program needs to connect to a server through a socket in order to work fine. 我的程序需要通过套接字连接到服务器才能正常工作。 I have created my program's docker image and its respective container, but when it gets to the following line it fails, and I have no idea why. 我已经创建了程序的docker映像及其相应的容器,但是当它到达以下行时会失败,并且我不知道为什么。

 sock.connect((host, port)) 

It shows this error message: 它显示此错误消息:

[Errno -2] Name or service not known [Errno -2]名称或服务未知

It runs just fine outside the containter. 它在容器外部运行良好。 I'm probably missing something really obvious but I can't see it. 我可能错过了一些确实很明显的东西,但我看不到。

Thanks in advance. 提前致谢。

Unless you've set it up in your /etc/hosts file of your docker container, it's unlikely that you're going to have the proper hostname setup. 除非您在docker容器的/etc/hosts文件中进行了设置,否则不太可能具有正确的主机名设置。

Luckily for you, Docker provides a nice way to expose this sort of information between two containers - environment variables. 幸运的是,Docker提供了一种在两个容器之间(环境变量)公开这种信息的好方法。 They're automatically exposed when you link two containers. 当您链接两个容器时,它们会自动显示。

In one terminal: 在一个终端中:

$ docker run --name camelot -it -p 5000 --rm python
Python 3.5.2 (default, Jul  8 2016, 19:17:03) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socketserver
>>> 
>>> class MyHandler(socketserver.BaseRequestHandler):
...     def handle(self):
...         self.data = self.request.recv(2048).strip()
...         print('{} wrote: '.format(self.client_address[0]))
...         print(self.data)
...         self.request.sendall(self.data.upper())
... 
>>> 
>>> server = socketserver.TCPServer(('0.0.0.0', 5000), MyHandler)
>>> server.serve_forever()

In a second: 一秒钟:

$ docker run --rm -it --link camelot python
Python 3.5.2 (default, Jul  8 2016, 19:17:03) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import socket
>>> 
>>> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> s.connect((os.environ['CAMELOT_PORT_5000_TCP_ADDR'],
...            int(os.environ['CAMELOT_PORT_5000_TCP_PORT'])))
>>> s.send(b'Hey dude!')
9
>>> print(s.recv(2048))
b'HEY DUDE!'
>>> s.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Dockerfile 连接到 docker 容器(python 服务器) - Can not connect to docker container(python server) with Dockerfile 如何在 docker 容器中安装 Python-dev? - How can i install Python-dev inside a docker container? 无法连接到 Python 中的 docker 容器内的 Selenium - Can't connect to Selenium inside docker container in Python Python Docker 容器得到 ProxyError,尽管我可以手动连接到服务器 - Python Docker Container gets ProxyError, despite I can connect to Server manually 使用python 2.7中的套接字连接到python服务器 - Connect to python server using sockets in python 2.7 如何将PyCharm连接到位于Docker容器内的python解释器? - How to connect PyCharm to a python interpreter located inside a Docker container? 如何使用Python中的ip地址将客户端通过套接字连接到服务器? - How to connect a client to a server with sockets, using ip adress in Python? 为什么我无法连接到docker容器中的flask服务器? - Why can't I connect to the flask server in docker container? 如何使用Python在Docker容器中获取主机ip? - How can I get the host ip in a docker container using Python? 如何使用Python访问Docker容器中的ScyllaDB? - How can I access ScyllaDB in Docker container using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM