Skip to content

Commit 3e11adf

Browse files
authored
Update README.md
1 parent 41a05fc commit 3e11adf

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,45 @@ ServerUrl为你的云函数地址
1818
将以下内容配置到云函数,并建议将云函数执行时间和api网关后端超时设置为15秒或以上:
1919

2020
```python
21-
from socket import *
21+
import socket
2222
from concurrent.futures import ThreadPoolExecutor
23+
from collections import defaultdict
2324
import threading
2425

25-
Tport = ""
26+
# 使用defaultdict存储每个线程找到的开放端口
27+
results_per_thread = defaultdict(list)
2628
ip = ""
2729
ports = ""
28-
lock = threading.Lock()
2930

3031
def Scan(port):
31-
global Tport
3232
try:
33-
conn = socket(AF_INET,SOCK_STREAM)
33+
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
3434
conn.settimeout(1)
35-
res = conn.connect_ex((str(ip),int(port)))
35+
res = conn.connect_ex((str(ip), int(port)))
3636
if res == 0:
37-
with lock:
38-
Tport = Tport + "," + port
37+
# 将结果添加到当前线程局部变量中
38+
results_per_thread[threading.current_thread()].append(port)
3939
except Exception as err:
4040
print(err)
4141
finally:
4242
conn.close()
4343

4444
def main_handler(event, context):
45-
global Tport,ip,ports
46-
45+
global ip, ports
46+
results_per_thread.clear()
4747
ip = event["queryString"]["ip"]
4848
ports = event["queryString"]["port"].split(",")
4949

50-
with ThreadPoolExecutor(max_workers = 20) as executor:
50+
with ThreadPoolExecutor(max_workers=20) as executor:
5151
executor.map(Scan, ports)
5252

53-
with lock:
54-
a = Tport[1:]
55-
Tport = ""
53+
# 合并所有线程的结果
54+
open_ports = []
55+
for thread_results in results_per_thread.values():
56+
open_ports.extend(thread_results)
57+
58+
# 将端口列表转换为逗号分隔的字符串
59+
a = ",".join(str(port) for port in open_ports)
5660
return a
5761
```
5862

0 commit comments

Comments
 (0)