python - 网络检查一直失败
问题描述
我试图让我导入到 health_checks.py 中的网络模块通过一切正常,但它一直失败。该模块独立工作,但我试图让 check_localhost 和 check_cpu_usage 打印,但它仍然没有打印任何东西。所以我目前无法调试代码来修复它并找出问题所在,因为每次我运行 health_checks.py 脚本时,它都会返回“网络检查失败”
health_checks.py 脚本的代码:
#!/usr/bin/env python3
from network import *
import shutil
import psutil
def check_disk_usage(disk):
"""Verifies that there's enough free space on disk"""
du = shutil.disk_usage(disk)
free = du.free / du.total * 100
return free > 20
def check_cpu_usage():
"""Verifies that there's enough unused CPU"""
usage = psutil.cpu_percent(1)
return usage < 75
# If there's not enough disk, or not enough CPU, print an error
if not check_disk_usage('/') or not check_cpu_usage():
print("ERROR!")
elif check_localhost() and check_connectivity():
print("Everything ok")
print(check_localhost())
print(check_cpu_usage())
else:
print("network checks failed")
network.py 的代码:
#!/usr/bin/env python3
import requests
import socket
def check_localhost():
localhost = socket.gethostbyname('localhost')
if localhost == '127.0.0.1':
return localhost
print(localhost)
def check_connectivity():
request = requests.get("http://www.google.com")
response = request.status_code
if response == '200':
return response
print(response)
我不知道这是一个导入问题还是内部脚本问题,任何帮助将不胜感激,谢谢。
解决方案
在这里,您
希望它可以帮助
PS 当您点击“检查进度按钮”时,它是否起作用了,因为它对我不起作用。它一直在向我展示工作
#!/usr/bin/env python3
import socket
import requests
localhost = socket.gethostbyname('localhost')
request = requests.get("http://www.google.com")
def check_localhost():
return localhost=='127.0.0.1'
def check_connectivity():
return request.status_code==200
推荐阅读
- javascript - 带有方法的属性的 Javascript 对象文字
- ruby-on-rails - Ruby on Rails 中多种类型用户的最佳实践是什么
- mocha.js - 使用 Typescript 编写的使用 NYC/Istanbul 的 Fastify 集成测试没有代码覆盖
- symfony - 使用 symfony/messenger 时切换 symfony/mailer 中的 sendmail 参数
- python - 从图像中去除噪声而不丢失 OpenCV 中的数据
- android - Invariant Violation:WebView 已从 React Native 中移除
- javascript - 尽管我使用了加载事件,img.height 随机返回 0
- flutter - 检测 TextFormField 停止输入颤动
- svelte - 为什么它不知道相同的内容?
- asp.net-core - 单击 blazor 中的按钮时如何动态添加新的输入字段