Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对于某些ip地址,python request.get在浏览器中不起作用,但在putty中起作用_Python - Fatal编程技术网

对于某些ip地址,python request.get在浏览器中不起作用,但在putty中起作用

对于某些ip地址,python request.get在浏览器中不起作用,但在putty中起作用,python,Python,我是python新手。我正在访问url。返回值的URL在request.get中工作正常,但不返回任何请求的URL.get不工作,所以我使用信号函数超时。“超时”在putty中工作得很好,但在浏览器中需要花费大量时间并返回500个错误 def handler(signum, frame): print("forever is over") raise Exception("end of time") def loop_forever(): import time imp

我是python新手。我正在访问url。返回值的URL在request.get中工作正常,但不返回任何请求的URL.get不工作,所以我使用信号函数超时。“超时”在putty中工作得很好,但在浏览器中需要花费大量时间并返回500个错误

def handler(signum, frame):
   print("forever is over")
   raise Exception("end of time")

def loop_forever():
   import time
   import requests
   url = "http://www.64.251.13.121/firewall/firewall_status.php"
   headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
   AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 
   Safari/537.36'}
   response = requests.get(url, headers=headers)
   if response.status_code == 200:
       print('Exists')
   else :
       print('Not Exists')



signal.signal(signal.SIGALRM, handler)
signal.alarm(3)


try:
   loop_forever()
except Exception:
   print('Timeout executed')
不应为请求设置超时参数。请为您做些什么

import requests
def check_url(url, timeout=3):
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
    try:
        response = requests.get(url, headers=headers, timeout=timeout)
    except requests.exceptions.Timeout:
       print(f'{url} time-out')
       return False
    except requests.exceptions.ConnectionError:
        print(f'{url} Connection Error')
        return False
    else:
        return response.status_code == 200 # what if redirect? is it possible

urls = [...]
for url in urls:
    print(f"{url} : {'Exists' if check_url(url=url) else 'Not Exists'}")

链接到

直接处理IP时,不需要提供前缀www


“为什么不去请求呢?”伯兰抱歉,我没听懂?
def loop_forever():
   import time
   import requests
   url = "http://172.217.215.138"  # This is google's IP 'for testing'. Change it with your valid IP
   headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
   response = requests.get(url, headers=headers)
   if response.status_code == 200:
       print('Exists')
   else :
       print('Not Exists')

loop_forever() # -> Valid