2. Contents
• Printing your machine's name and IPv4
address
• Retrieving a remote machine's IP address
• Converting an IPv4 address to dierent
formats
• Finding a service name, given the port and
protocol
• Converting integers to and from host to
network byte order
3. Printing your machine's name and
IPv4 address
C:UsersUser>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> host_name = socket.gethostname()
>>> print "Host name : %s" %host_name
Host name : 이태영-PC
>>> print "IP address : %s" %socket.gethostbyname(host_name)
IP address : 169.254.236.217
4.
5. Quiz 1
• 자신의 PC 이름과 IPv4 address를 출력할
함수를 만들어 구현하기
12. Converting an IPv4 address to
dierent formats
• 파이썬 소켓 라이브러리는 다양한 IP 주소 형식을
다루는 유틸리티가 있습니다.
• 우리가 두 가지 사용 :
– inet_aton ()
– inet_ntoa ()
• convert_ip4_address () 함수, inet_aton을 만들어 보자
• inet_ntoa ()는 IP 주소 변환을 위해 사용된다.
• 우리는 두 개의 샘플 IP를 사용합니다
– 주소, 127.0.0.1과 192.168.0.1.
14. Finding a service name, given the
port and protocol
우리가 find_service_name () 함수를 정의
• 여기서 getservbyport를 () 소켓 클래스 함수
는, (25)는, 예를 들면, 몇 개의 포트 (80)를 호
출
• 포트에 대한 서비스를 찾기 위한 함수를 구현
한다.
16. Converting integers to and from
host to network byte order
• 파이썬의 소켓 라이브러리는 바이트 순서와
그 반대를 호스팅하는 네트워크 바이트 순서
로 변환하는 유틸리티가 있습니다.
• ntohl () / htonl() 함수를 사용
• ntohl () / htonl () 소켓 클래스 함수는 IP 어
드레스 포맷을 변환하기 위해 사용
• convert_integer () 함수를 정의하자.