KEMBAR78
Python Network Programming | PPTX
Python Network
Programming
Produced by Tae Young Lee
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
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
Quiz 1
• 자신의 PC 이름과 IPv4 address를 출력할
함수를 만들어 구현하기
정답
Retrieving a remote machine's IP
address
• https://docs.python.org/3/library/socket.h
tml
• built-in library function
– gethostbyname()
Remote machine’s IP
Quiz 2
• Remote machine의 IP주소를 가져올 수 있
는 함수 구현하기
HINT
정답
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.
정답
Finding a service name, given the
port and protocol
우리가 find_service_name () 함수를 정의
• 여기서 getservbyport를 () 소켓 클래스 함수
는, (25)는, 예를 들면, 몇 개의 포트 (80)를 호
출
• 포트에 대한 서비스를 찾기 위한 함수를 구현
한다.
정답
Converting integers to and from
host to network byte order
• 파이썬의 소켓 라이브러리는 바이트 순서와
그 반대를 호스팅하는 네트워크 바이트 순서
로 변환하는 유틸리티가 있습니다.
• ntohl () / htonl() 함수를 사용
• ntohl () / htonl () 소켓 클래스 함수는 IP 어
드레스 포맷을 변환하기 위해 사용
• convert_integer () 함수를 정의하자.
정답

Python Network Programming

  • 1.
  • 2.
    Contents • Printing yourmachine'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'sname 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
  • 5.
    Quiz 1 • 자신의PC 이름과 IPv4 address를 출력할 함수를 만들어 구현하기
  • 6.
  • 7.
    Retrieving a remotemachine's IP address • https://docs.python.org/3/library/socket.h tml • built-in library function – gethostbyname()
  • 8.
  • 9.
    Quiz 2 • Remotemachine의 IP주소를 가져올 수 있 는 함수 구현하기
  • 10.
  • 11.
  • 12.
    Converting an IPv4address 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.
  • 13.
  • 14.
    Finding a servicename, given the port and protocol 우리가 find_service_name () 함수를 정의 • 여기서 getservbyport를 () 소켓 클래스 함수 는, (25)는, 예를 들면, 몇 개의 포트 (80)를 호 출 • 포트에 대한 서비스를 찾기 위한 함수를 구현 한다.
  • 15.
  • 16.
    Converting integers toand from host to network byte order • 파이썬의 소켓 라이브러리는 바이트 순서와 그 반대를 호스팅하는 네트워크 바이트 순서 로 변환하는 유틸리티가 있습니다. • ntohl () / htonl() 함수를 사용 • ntohl () / htonl () 소켓 클래스 함수는 IP 어 드레스 포맷을 변환하기 위해 사용 • convert_integer () 함수를 정의하자.
  • 17.