python network programming
TRANSCRIPT
![Page 1: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/1.jpg)
Python Network Program-ming
Produced by Tae Young Lee
![Page 2: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/4.jpg)
![Page 5: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/5.jpg)
Quiz 1• 자신의 PC 이름과 IPv4 address 를 출력할 함수를 만들어 구현하기
![Page 6: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/6.jpg)
정답
![Page 7: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/7.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/8.jpg)
Remote machine’s IP
![Page 9: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/9.jpg)
Quiz 2• Remote machine 의 IP 주소를 가져올 수 있는 함수 구현하기
![Page 10: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/10.jpg)
HINT
![Page 11: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/11.jpg)
정답
![Page 12: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/13.jpg)
정답
![Page 14: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/14.jpg)
Finding a service name, given the port and protocol
우리가 find_service_name () 함수를 정의• 여기서 getservbyport 를 () 소켓 클래스 함수는 , (25) 는 , 예를 들면 , 몇 개의 포트
(80) 를 호출• 포트에 대한 서비스를 찾기 위한 함수를 구현한다 .
![Page 15: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/15.jpg)
정답
![Page 16: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/16.jpg)
Converting integers to and from host to network byte order
• 파이썬의 소켓 라이브러리는 바이트 순서와 그 반대를 호스팅하는 네트워크 바이트 순서로 변환하는 유틸리티가 있습니다 .
• ntohl () / htonl() 함수를 사용• ntohl () / htonl () 소켓 클래스 함수는 IP 어드레스 포맷을 변환하기 위해 사용• convert_integer () 함수를 정의하자 .
![Page 17: Python Network Programming](https://reader036.vdocuments.us/reader036/viewer/2022083104/5882d0dc1a28abf8388b5d2f/html5/thumbnails/17.jpg)
정답