Skip to content

Commit

Permalink
[김필모] 10주차 미션 제출 (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
why-arong authored Jul 9, 2024
1 parent c6fff81 commit 5261d02
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions 8th_members/김필모/10주차.md
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">


0 comments on commit 5261d02

Please sign in to comment.