Last active
July 22, 2022 01:03
-
-
Save ssrlive/15d4f5ab42e94d1297a33d57bc324de9 to your computer and use it in GitHub Desktop.
host : port is accessable from china
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| beautifulsoup4>=4.11.1 | |
| requests-html>=0.10.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pip3 install -r requirements.txt