Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

클라이언트의 IP 주소를 찾는 Python 프로그램

<시간/>

이 튜토리얼에서는 IP 소켓을 사용하는 클라이언트의 주소 Python의 모듈 . 모든 노트북, 모바일, 태블릿 등에는 고유한 IP가 있습니다. 주소. socket 모듈을 사용하여 찾을 것입니다. IP를 찾는 단계를 살펴보겠습니다. 장치의 주소입니다.

알고리즘

1. Import the socket module.
2. Get the hostname using the socket.gethostname() method and store it in a variable.
3. Find the IP address by passing the hostname as an argument to the
socket.gethostbyname() method and store it in a variable.
4. Print the IP address.

위의 알고리즘에 대한 코드를 작성해 봅시다.

## importing socket module
import socket
## getting the hostname by socket.gethostname() method
hostname = socket.gethostname()
## getting the IP address using socket.gethostbyname() method
ip_address = socket.gethostbyname(hostname)
## printing the hostname and ip_address
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")

출력

위의 프로그램을 실행하면 다음과 같은 결과를 얻을 수 있습니다.

Hostname: DESKTOP-A0PM5GD
IP Address: 192.168.43.15

결론

튜토리얼에 대해 궁금한 점이 있으면 댓글 섹션에 언급하세요.