Skip to content

Commit 5261d02

Browse files
authored
[김필모] 10주차 미션 제출 (#117)
1 parent c6fff81 commit 5261d02

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

8th_members/김필모/10주차.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Port Scanner 예제 실행해보기
2+
3+
### 싱글 프로세스 + 싱글 스레드
4+
```python3
5+
from queue import Queue
6+
import socket
7+
import time
8+
9+
timeout = 1.0
10+
11+
def check_port(host: str, port: int, results: Queue):
12+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
13+
sock.settimeout(timeout)
14+
result = sock.connect_ex((host, port))
15+
if result == 0:
16+
results.put(port)
17+
sock.close()
18+
19+
def main():
20+
start = time.time()
21+
host = "localhost" # Replace with a host you own
22+
results = Queue()
23+
for port in range(30000, 65536):
24+
check_port(host, port, results)
25+
while not results.empty():
26+
print("Port {0} is open".format(results.get()))
27+
print("Completed scan in {0} seconds".format(time.time() - start))
28+
29+
if __name__ == '__main__':
30+
main()
31+
```
32+
<img width="831" alt="Screenshot 2024-06-30 at 2 44 52 PM" src="https://github.com/why-arong/CPython-Guide/assets/68311908/2860fa95-7d0c-447b-815a-f0e3effb4fd2">
33+
34+
35+
### 멀티스레딩
36+
37+
38+
```python3
39+
from threading import Thread
40+
from queue import Queue
41+
import socket
42+
import time
43+
44+
timeout = 1.0
45+
46+
def check_port(host: str, port: int, results: Queue):
47+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48+
sock.settimeout(timeout)
49+
result = sock.connect_ex((host, port))
50+
if result == 0:
51+
results.put(port)
52+
sock.close()
53+
54+
def main():
55+
start = time.time()
56+
host = "localhost" # Replace with a host you own
57+
threads = []
58+
results = Queue()
59+
for port in range(30000, 65536):
60+
t = Thread(target=check_port, args=(host, port, results))
61+
t.start()
62+
threads.append(t)
63+
for t in threads:
64+
t.join()
65+
while not results.empty():
66+
print("Port {0} is open".format(results.get()))
67+
print("Completed scan in {0} seconds".format(time.time() - start))
68+
69+
if __name__ == '__main__':
70+
main()
71+
```
72+
<img width="894" alt="Screenshot 2024-06-30 at 2 45 36 PM" src="https://github.com/why-arong/CPython-Guide/assets/68311908/1af4f0cd-182c-4474-949e-b58c2ebd388b">
73+
74+
75+
### 멀티스레딩 + 스레드 개수 제한
76+
```python3
77+
import socket
78+
import time
79+
from concurrent.futures import ThreadPoolExecutor, as_completed
80+
from queue import Queue
81+
82+
timeout = 1.0
83+
84+
85+
def check_port(host: str, port: int) -> int:
86+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
87+
sock.settimeout(timeout)
88+
result = sock.connect_ex((host, port))
89+
if result == 0:
90+
return port
91+
return None
92+
93+
94+
def main():
95+
start = time.time()
96+
host = "localhost" # Replace with a host you own
97+
open_ports = []
98+
99+
with ThreadPoolExecutor(max_workers=50) as executor:
100+
futures = [executor.submit(check_port, host, port)
101+
for port in range(30000, 65536)]
102+
103+
for future in as_completed(futures):
104+
port = future.result()
105+
if port is not None:
106+
open_ports.append(port)
107+
108+
for port in open_ports:
109+
print(f"Port {port} is open")
110+
print(f"Completed scan in {time.time() - start:.2f} seconds")
111+
112+
113+
if __name__ == '__main__':
114+
main()
115+
116+
```
117+
118+
<img width="871" alt="Screenshot 2024-06-30 at 2 46 34 PM" src="https://github.com/why-arong/CPython-Guide/assets/68311908/74eb275c-56ad-4307-84fe-6395b388ddc1">
119+
120+

0 commit comments

Comments
 (0)