我是新鲜的,使用Python,我在python 3中遇到了这个挑战。 1 - 检查互联网连接是否正常。 2 - 如果条件1为真,则调用服务 3 - 重复
此代码用于检查ping是否正常工作:
import os
hostname = ('https://fmolinaclouds0000000000trial.hanatrial.ondemand.com/demoApp/demo0
1/app02/SHIOT_02/services/putSensorReading.xsjs?id=TEST&value=999')
while true:
response = os.system('ping -c 1 ' + hostname)
#and then check the response...
if response == 0:
print('is up!')
else:
print('is down!')
这是调用服务的代码:
from sense_hat import SenseHat
sense = SenseHat()
import requests
import time
temp = sense.temp
payload_temp = {'id': 'TEMP', 'value': sense.get_temperature()}
payload_press = {'id': 'PRESS', 'value': sense.get_pressure()}
payload_humit = {'id': 'HUMIT', 'value': sense.get_humidity()}
payload_ts = {'id': 'TS', 'value': time.time()}
while True:
# Wait for 60 seconds
temp = sense.temp
requests.get('https://fmolinaclouds0008215086trial.hanatrial.ondemand.com/demoApp/demo01/app02/SHIOT_02/services/putSensorReading.xsjs', params=payload_temp)
r = requests.get('https://fmolinaclouds0008215086trial.hanatrial.ondemand.com/demoApp/demo01/app02/SHIOT_02/services/putSensorReading.xsjs', params=payload_press)
r = requests.get('https://fmolinaclouds0008215086trial.hanatrial.ondemand.com/demoApp/demo01/app02/SHIOT_02/services/putSensorReading.xsjs', params=payload_humit)
print(payload_temp)
print(payload_press)
print(payload_humit)
# print(payload_ts)
time.sleep(60)
那么,如何把所有这些放在一起呢?
答案 0 :(得分:0)
您需要定义一个函数,然后从主程序中调用它,例如
def check_internet(hostname):
return os.system("ping --count 1 " + hostname) == 0:
# step 1
internet_okay = check_internet("localhost")
if internet_okay: # step 2
do_other_stuff() # step3
如需更多帮助,请查看these directions。