python network programming

17
Python Network Programming Produced by Tae Young Lee

Upload: tae-young-lee

Post on 21-Jan-2017

616 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Python Network Programming

Python Network Program-ming

Produced by Tae Young Lee

Page 2: Python Network Programming

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

Page 3: Python Network Programming

Printing your machine's name andIPv4 address

C:\Users\User>pythonPython 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32

bit (Intel)] on win32Type "help", "copyright", "credits" or "license" for more infor-

mation.>>> import socket>>> host_name = socket.gethostname()>>> print "Host name : %s" %host_nameHost name : 이태영 -PC>>> print "IP address : %s"

%socket.gethostbyname(host_name)IP address : 169.254.236.217

Page 4: Python Network Programming
Page 5: Python Network Programming

Quiz 1• 자신의 PC 이름과 IPv4 address 를 출력할 함수를 만들어 구현하기

Page 6: Python Network Programming

정답

Page 7: Python Network Programming

Retrieving a remote machine's IP address

• https://docs.python.org/3/library/socket.html

• built-in library function– gethostbyname()

Page 8: Python Network Programming

Remote machine’s IP

Page 9: Python Network Programming

Quiz 2• Remote machine 의 IP 주소를 가져올 수 있는 함수 구현하기

Page 10: Python Network Programming

HINT

Page 11: Python Network Programming

정답

Page 12: Python Network Programming

Converting an IPv4 address to dier-ent formats

• 파이썬 소켓 라이브러리는 다양한 IP 주소 형식을 다루는 유틸리티가 있습니다 .

• 우리가 두 가지 사용 :– inet_aton ()– inet_ntoa ()

• convert_ip4_address () 함수 , inet_aton을 만들어 보자• inet_ntoa ()는 IP 주소 변환을 위해 사용된다 .

• 우리는 두 개의 샘플 IP를 사용합니다– 주소 , 127.0.0.1과 192.168.0.1.

Page 13: Python Network Programming

정답

Page 14: Python Network Programming

Finding a service name, given the port and protocol

우리가 find_service_name () 함수를 정의• 여기서 getservbyport 를 () 소켓 클래스 함수는 , (25) 는 , 예를 들면 , 몇 개의 포트

(80) 를 호출• 포트에 대한 서비스를 찾기 위한 함수를 구현한다 .

Page 15: Python Network Programming

정답

Page 16: Python Network Programming

Converting integers to and from host to network byte order

• 파이썬의 소켓 라이브러리는 바이트 순서와 그 반대를 호스팅하는 네트워크 바이트 순서로 변환하는 유틸리티가 있습니다 .

• ntohl () / htonl() 함수를 사용• ntohl () / htonl () 소켓 클래스 함수는 IP 어드레스 포맷을 변환하기 위해 사용• convert_integer () 함수를 정의하자 .

Page 17: Python Network Programming

정답