samedi 27 mars 2021

How to continue python programme when a webpage is not reachable

Hello Stackoverflow members, I am very new to python hence i need a little bit of help with this small dilemma i have come across, what needs to be done is: if 10.10.10.2 is reachable the programm should exit() but if 10.10.10.2 is not reachable in 10 seconds 192.168.100.5 will be opened and the programm will continue... If anyone could give a small example as to how that could be done it would be much appreciated.

import requests
import urllib.request
import eventlet
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
from xml.dom import minidom
import urllib.request
import time

preflash = urllib.request.urlopen("http://10.10.10.2").getcode()
correct = urllib.request.urlopen("http://192.168.100.5").getcode()

print(100*"#")

#eventlet.monkey_patch()
#with eventlet.Timeout(10):

if True:
         print("Web page status code:",preflash)
         print("Web page is reachable")

else:    
    print("Web page status code:", correct)
    print("Web page is reachable")
url_str = 'http://192.168.100.2/globals.xml'

# open webpage and read values
xml_str = urllib.request.urlopen(url_str).read()

# Parses XML doc to String for Terminal output
xmldoc = minidom.parseString(xml_str)

# Finding the neccassary Set points/ Sollwerte from the xmldoc

# prints the order_number from the xmldoc
order_number = xmldoc.getElementsByTagName('order_number')
print("The Order number of the current device is:", order_number[0].firstChild.nodeValue)
print(100*"-") 

Aucun commentaire:

Enregistrer un commentaire