generated from Pseudo-Lab/Jupyter-Book-Template
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
# Port Scanner 예제 실행해보기 | ||
|
||
### 싱글 프로세스 + 싱글 스레드 | ||
```python3 | ||
from queue import Queue | ||
import socket | ||
import time | ||
|
||
timeout = 1.0 | ||
|
||
def check_port(host: str, port: int, results: Queue): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.settimeout(timeout) | ||
result = sock.connect_ex((host, port)) | ||
if result == 0: | ||
results.put(port) | ||
sock.close() | ||
|
||
def main(): | ||
start = time.time() | ||
host = "localhost" # Replace with a host you own | ||
results = Queue() | ||
for port in range(30000, 65536): | ||
check_port(host, port, results) | ||
while not results.empty(): | ||
print("Port {0} is open".format(results.get())) | ||
print("Completed scan in {0} seconds".format(time.time() - start)) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
``` | ||
<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"> | ||
|
||
|
||
### 멀티스레딩 | ||
|
||
|
||
```python3 | ||
from threading import Thread | ||
from queue import Queue | ||
import socket | ||
import time | ||
|
||
timeout = 1.0 | ||
|
||
def check_port(host: str, port: int, results: Queue): | ||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
sock.settimeout(timeout) | ||
result = sock.connect_ex((host, port)) | ||
if result == 0: | ||
results.put(port) | ||
sock.close() | ||
|
||
def main(): | ||
start = time.time() | ||
host = "localhost" # Replace with a host you own | ||
threads = [] | ||
results = Queue() | ||
for port in range(30000, 65536): | ||
t = Thread(target=check_port, args=(host, port, results)) | ||
t.start() | ||
threads.append(t) | ||
for t in threads: | ||
t.join() | ||
while not results.empty(): | ||
print("Port {0} is open".format(results.get())) | ||
print("Completed scan in {0} seconds".format(time.time() - start)) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
``` | ||
<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"> | ||
|
||
|
||
### 멀티스레딩 + 스레드 개수 제한 | ||
```python3 | ||
import socket | ||
import time | ||
from concurrent.futures import ThreadPoolExecutor, as_completed | ||
from queue import Queue | ||
|
||
timeout = 1.0 | ||
|
||
|
||
def check_port(host: str, port: int) -> int: | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: | ||
sock.settimeout(timeout) | ||
result = sock.connect_ex((host, port)) | ||
if result == 0: | ||
return port | ||
return None | ||
|
||
|
||
def main(): | ||
start = time.time() | ||
host = "localhost" # Replace with a host you own | ||
open_ports = [] | ||
|
||
with ThreadPoolExecutor(max_workers=50) as executor: | ||
futures = [executor.submit(check_port, host, port) | ||
for port in range(30000, 65536)] | ||
|
||
for future in as_completed(futures): | ||
port = future.result() | ||
if port is not None: | ||
open_ports.append(port) | ||
|
||
for port in open_ports: | ||
print(f"Port {port} is open") | ||
print(f"Completed scan in {time.time() - start:.2f} seconds") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() | ||
|
||
``` | ||
|
||
<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"> | ||
|
||
|