gpt4 book ai didi

用于在网站上查找事件端口的 Python 程序?

转载 作者:太空宇宙 更新时间:2023-11-04 04:41:07 31 4
gpt4 key购买 nike

我的大学有一些端口。像这样的东西 http://www.college.in:913我想要一个程序来找到活跃的。我的意思是我想要网站运行的那些端口号。这是一个代码。但这需要很多时间。

from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError
for i in range(1,10000):
req = Request("http://college.edu.in:"+str(i))
try:
response = urlopen(req)

except URLError as e:
print("Error at port"+str(i) )
else:
print ('Website is working fine'+str(i))

最佳答案

尝试打开到范围内每个端口的套接字连接可能会更快,然后仅在套接字实际打开时才尝试发出请求。但是迭代一堆端口通常很慢。如果每次都需要 0.5 秒,而您正在扫描 10000 个端口,那将需要大量的等待时间。

# create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# now connect to the web server on port 80 - the normal http port
s.connect(("www.python.org", 80))
s.close()

来自 https://docs.python.org/3/howto/sockets.html

您还可以考虑分析代码并找出速度慢的部分。

关于用于在网站上查找事件端口的 Python 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50606523/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com