# !/usr/bin/python3
# coding: utf-8
import certifi
import pycurl
def ping_to_website(website, interface):
"""
website like https://www.baidu.com/
interface like 192.168.1.1
"""
c = pycurl.Curl()
try:
c.setopt(pycurl.URL, website)
c.setopt(pycurl.CAINFO, certifi.where()) # for https, also supported HTTP
c.setopt(pycurl.USERAGENT, "Chrome/91.0.4472.77")
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 1)
c.setopt(pycurl.TIMEOUT, 1) # too strict
if interface:
c.setopt(pycurl.INTERFACE, interface)
c.setopt(pycurl.WRITEFUNCTION, lambda i: None) # avoid print too much
c.perform() # default to print HTML content
code = c.getinfo(pycurl.HTTP_CODE)
if 200 == code:
return True
return False
except pycurl.error as e:
return False
finally:
if c:
c.close()