Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Last active July 22, 2022 01:03
Show Gist options
  • Save ssrlive/15d4f5ab42e94d1297a33d57bc324de9 to your computer and use it in GitHub Desktop.
Save ssrlive/15d4f5ab42e94d1297a33d57bc324de9 to your computer and use it in GitHub Desktop.
host : port is accessable from china
from time import sleep
from bs4 import BeautifulSoup
from requests_html import HTMLSession
def host_port_accessable_from_china(host: str, port: int, delay: bool) -> bool:
"""
test if the host:port is accessable from China
"""
url = "https://tool.chinaz.com/port?host={}&port={}".format(host, port)
# initialize the session
session = HTMLSession()
r = session.get(url)
# for javascript driven websites
r.html.render()
if delay:
sleep(1)
soup = BeautifulSoup(r.html.html, 'html.parser')
title = soup.title.text
if title != "{}网站端口扫描结果".format(host):
print("{} is not a accessable website".format(host))
return False
# get all spans with class "portitem"
spans = soup.find_all("span", class_="portitem")
if len(spans) != 1:
print("{} is not a accessable website".format(host))
return False
port_span = spans[0]
target_port = port_span.text
if target_port != str(port):
print("{} is not a accessable website".format(host))
return False
spans2 = port_span.find_next_siblings("span")
if len(spans2) == 0:
print("{} is not a accessable website".format(host))
return False
status_span = spans2[0]
status = status_span.text
if status != "开启":
print("{} is not a accessable website".format(host))
return False
return True
if __name__ == "__main__":
host = "sina.com.cn"
port = 443
result = host_port_accessable_from_china(host, port, False)
r_txt = "accessable" if result else "unaccessable"
print("{}:{} is {} from China".format(host, port, r_txt))
beautifulsoup4>=4.11.1
requests-html>=0.10.0
@ssrlive
Copy link
Author

ssrlive commented Jul 22, 2022

pip3 install -r requirements.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment