python network programming
Post on 21-Jan-2017
616 Views
Preview:
TRANSCRIPT
Python Network Program-ming
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 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
Quiz 1• 자신의 PC 이름과 IPv4 address 를 출력할 함수를 만들어 구현하기
정답
Retrieving a remote machine's IP address
• https://docs.python.org/3/library/socket.html
• built-in library function– gethostbyname()
Remote machine’s IP
Quiz 2• Remote machine 의 IP 주소를 가져올 수 있는 함수 구현하기
HINT
정답
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.
정답
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 () 함수를 정의하자 .
정답
top related