如何解决无法在服务器上的 Docker 容器内建立网络连接 [Errno 111]
我在本地计算机上有一个可用的 python 脚本,它可以在没有 Docker 的服务器上运行,它使用来自 twitter api 的 twitter 数据(我不使用 tweepy)。
当我尝试从我的 CentOS 服务器 Linode 上的 docker 容器运行它时,我得到
ConnectionError(<urllib3.connection.HTTPConnection object at 0x7fab73c152d0>: Failed to establish a new connection: [Errno 111] Connection refused) caused by: NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7fab73c152d0>: Failed to establish a new connection: [Errno 111] Connection refused)
尽管如此,我可以成功地执行 requests.post
(即我的不记名令牌是正确的),我这样做是为了更新我的 Twitter 规则。那么为什么我不能做requests.get
?
我需要在服务器上打开一个端口吗?和 docker 容器?
这是我的流函数:
def get_stream(headers,sleep=10):
include_fields = ",".join(["author_id","created_at","public_metrics"])
url_with_query_params = (
"https://api.twitter.com/2/tweets/search/stream?tweet.fields={}".format(
include_fields
)
)
response = requests.get(
url_with_query_params,headers=headers,stream=True,)
if response.status_code != 200:
if sleep >= 500:
sys.exit("Just couldn't connect.",response.status_code,response.text)
time.sleep(sleep)
get_stream(headers,sleep + 2 * sleep)
latest_tweet = datetime.now()
for response_line in response.iter_lines():
if response_line:
json_response = json.loads(response_line)
try:
result = json_response["data"].copy()
result["polarity_scores"] = sia.polarity_scores(result["text"])
result["matching_rules"] = json_response["matching_rules"]
es.index(index='tweets',id=result["id"],body=result)
print(result['id'])
except Exception as e:
print(e)
这是 Dockerfile:
FROM python:3.7-alpine
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
COPY secrets.json secrets.json
RUN pip3 install -r requirements.txt
COPY filtered-stream.py filtered-stream.py
RUN python3 -m nltk.downloader vader_lexicon
EXPOSE 80
EXPOSE 443
EXPOSE 8080
CMD ["python","filtered-stream.py"]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。