gpt4 book ai didi

仅在 docker 环境中出现 Python 错误(名称或服务未知)

转载 作者:行者123 更新时间:2023-12-02 18:05:55 26 4
gpt4 key购买 nike

我在 docker 容器中运行了一个 python 脚本,它应该会向我发送一封包含结果的电子邮件。

在没有 docker 环境的本地机器上它可以工作,但在容器中它给了我这个:

grab_immo    | Traceback (most recent call last):
grab_immo | File "./main.py", line 236, in <module>
grab_immo | main()
grab_immo | File "./main.py", line 229, in main
grab_immo | sendmail(item['link'], item['price'], item['titel'], item['description'])
grab_immo | File "./main.py", line 76, in sendmail
grab_immo | s = smtplib.SMTP(host=SMTP_HOST, port=SMTP_PORT)
grab_immo | File "/usr/local/lib/python3.7/smtplib.py", line 251, in __init__
grab_immo | (code, msg) = self.connect(host, port)
grab_immo | File "/usr/local/lib/python3.7/smtplib.py", line 336, in connect
grab_immo | self.sock = self._get_socket(host, port, self.timeout)
grab_immo | File "/usr/local/lib/python3.7/smtplib.py", line 307, in _get_socket
grab_immo | self.source_address)
grab_immo | File "/usr/local/lib/python3.7/socket.py", line 707, in create_connection
grab_immo | for res in getaddrinfo(host, port, 0, SOCK_STREAM):
grab_immo | File "/usr/local/lib/python3.7/socket.py", line 748, in getaddrinfo
grab_immo | for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
grab_immo | socket.gaierror: [Errno -2] Name or service not known
grab_immo exited with code 0

导致错误的部分:

def sendmail(link, price, titel, description):
logger.info('Attempt to send mails with object %s', link)
names, emails = get_contacts(newPath + 'contacts') # read contacts
message_template = read_template(newPath + 'template')

# set up the SMTP server
s = smtplib.SMTP(host=SMTP_HOST, port=SMTP_PORT)
s.starttls()
s.login(MY_ADDRESS, PASSWORD)

# For each contact, send the email:
for name, email in zip(names, emails):
msg = MIMEMultipart() # create a message

# add in the actual person name to the message template
message = message_template.substitute(PERSON_NAME=name.title(), LINK=link, PRICE=price, TITEL=titel, DESCRIPTION=description)

# setup the parameters of the message
msg['From']=MY_ADDRESS
msg['To']=email
msg['Subject']="yyy"

# add in the message body
msg.attach(MIMEText(message, 'plain'))

# send the message via the server set up earlier.
s.send_message(msg)
del msg
logger.info('Mail sent to %s', name)

# Terminate the SMTP session and close the connection
s.quit()

环境部分:

MY_ADDRESS = os.environ['MY_ADDRESS']
PASSWORD = os.environ['PASSWORD']
SMTP_HOST = os.environ['SMTP_HOST']
SMTP_PORT = os.environ['SMTP_PORT']

I had another problem before, but I dont think it's related

感谢阅读!

编辑:docker-compose 部分

environment:
- MY_ADDRESS = yyy@yyy.com
- PASSWORD = yyy
- SMTP_HOST = smtp.strato.de
- SMTP_PORT = 587

取自here

docker 文件:

FROM python:3

COPY grab_immo /grab_immo

RUN mkdir /data

ADD ./data/contacts /data
ADD ./data/run.log /data
ADD ./data/template /data

VOLUME /data

WORKDIR /grab_immo

RUN pip install -r requirements.txt

ENV MY_ADDRESS=value \
PASSWORD=value \
SMTP_HOST=value \
SMTP_PORT=587

CMD [ "python", "./main.py" ]

docker-compose.yml

---
version: "2"
services:
grab_immo:
build: .
image: yyy/grab_immo
container_name: grab_immo
environment:
- MY_ADDRESS = 'stuff'
- PASSWORD = stuff
- SMTP_HOST = smtp.strato.de
- SMTP_PORT = 587
volumes:
- /home/yyy/grab_immo_config:/data
restart: unless-stopped

最佳答案

问题是您没有从您提供的 ENV 中获取值试试这个:

import os
host = os.environ['SMTP_HOST']
port = os.environ['SMTP_PORT']
s = smtplib.SMTP(host=host, port=port)

关于仅在 docker 环境中出现 Python 错误(名称或服务未知),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58421206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com