60
Python 으으 으으으으으 HTTP 으으

GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Embed Size (px)

Citation preview

Page 1: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Python 으로 만들어보는 HTTP 서버

Page 2: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

강성일 (Luavis) |[email protected]. 1995 Seoul, KR

Junior Software EngineerWeb Front/Back-end / iPhone Developer / Emebed DeveloperTechnical ToolsPython, Swift, PHP, JavaScript(Node.JS/React), Objective-C, HTML, Linux, C, TCP socket, MariaDB, NGINX, flask, AVR, Freescale, ...Professional Experience• Freelancer Aug, 2015 to now • iPhone Developer, Feb, 2014 to Aug, 2014, Hoit company – Seoul, KR Achievement• Korea Olympiad in Informatics, in 2013• Hanyang univ, Smart car competition in 2015

Research Papers• Lane Detection and Tracking Based on Line Scan Camera in Autonomous Driving Miniature Vehicle, 2015

Education• Soongsil Univ, Since 2014 – Seoul, KR

In college of Electronic Engineering• SW Maestro 6th, 2015 - 2016 – Seoul, KR• Korea Digital Media High school, 2011 - 2013 – Ansan, KR

Major in Web-promgramming

Page 3: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

선수지식

Python Socket

Python 으로 만들어보는 HTTP 서버

Flask

Page 4: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

배경

Codelab

Python 으로 만들어보는 HTTP 서버

Python 을 이용해서 어떻게 HTTP 서버를 만들까

Page 5: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

배경

HTTP 의 구조 / HTTP2이걸 Python 으로 짜둔소스 조금 설명 ?

Page 6: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

기본 지식

HTTP

TCP/IP

여기부터는사실상하드웨어

Page 7: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

기본 지식

HTMLHyper Text Markup Language

Page 8: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

기본 지식

Hyper TextTransfer하이퍼 텍스트를 옮기는

Protocol프로토콜

Page 9: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

기본 지식

Request

Client Server

Response

Page 10: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Request

Request

Page 11: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Request packet

POST / HTTP/1.1Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip, deflate, sdchAccept-Language:ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2Cache-Control:max-age=0Connection:keep-aliveHost:www.w3.orgReferer:https://www.google.co.kr/User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36

search=google&utf-8=&page=10

Packet 구조

Page 12: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Request packet

POST / HTTP/1.1Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip, deflate, sdchAccept-Language:ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2Cache-Control:max-age=0Connection:keep-aliveHost:www.w3.orgReferer:https://www.google.co.kr/User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36

search=google&utf-8=&page=10

Page 13: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Request packet

POST / HTTP/1.1Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip, deflate, sdchAccept-Language:ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2Cache-Control:max-age=0Connection:keep-aliveHost:www.w3.orgReferer:https://www.google.co.kr/User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36

search=google&utf-8=&page=10

Packet 구조

Page 14: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Request packet

POST / HTTP/1.1Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Accept-Encoding:gzip, deflate, sdchAccept-Language:ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4,ja;q=0.2Cache-Control:max-age=0Connection:keep-aliveHost:www.w3.orgReferer:https://www.google.co.kr/User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36

search=google&utf-8=&page=10

Page 15: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Request packet

POST / HTTP/1.1Method

Path

Version

Page 16: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Method

GET

HEAD

POST

PUT

DELETE

TRACE

OPTIONS

CONNECT

PATCHGeneral usage

POST / HTTP/1.1Method

Path

Version

Page 17: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Method

GET

HEAD

POST

PUT

DELETE

TRACE

OPTIONS

CONNECT

PATCHRestful usage

POST / HTTP/1.1Method

Path

Version

Page 18: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Method

GET

HEAD

PUT

DELETE

OPTIONS

CONNECT

자료를 요청

자료의 존재 요청

POST TRACE자료 업로드

자료 수정

자료 삭제

서버 확인

가능한 메소드 확인

PATCH부분적 수정

Page 19: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Path

POST / HTTP/1.1Method

Path

Version

Page 20: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Path

https://www.pycon.kr/2016apac/about/coc/

Page 21: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Path

/2016apac/about/coc/https:// www.pycon.kr프로토콜

도메인

Path( 경로 )

Page 22: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Versions

- Berners-Lee first proposed the "WorldWideWeb" project in 1989. - The first documented version of HTTP was HTTP V0.9 (1991).

- Dave Raggett led the HTTP Working Group (HTTP WG) in 1995. - RFC 1945 officially introduced and recognized HTTP V1.0 in 1996.

- The HTTP/1.1, RFC 2068 was officially released in January 1997

Page 23: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

정리해보면

GET /2016apac/about/coc/ HTTP/1.1자료 요청

이런 경로에 있는걸

HTTP 1.1 프로토콜로

Page 24: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Response

Response

Page 25: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Response packet

HTTP/1.1 200 OKContent-Language:enContent-Location:O-HTTP-charset.en.phpContent-Type:text/html; charset=UTF-8Date:Tue, 19 May 2015 15:23:39 GMTP3P:policyref="http://www.w3.org/2014/08/p3p.xml"Server:Apache/2TCN:choiceTransfer-Encoding:chunkedVary:negotiate,accept-language

<html><p>hello World</p></html>

Page 26: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Response packet

HTTP/1.1 200 OKContent-Language:enContent-Location:O-HTTP-charset.en.phpContent-Type:text/html; charset=UTF-8Date:Tue, 19 May 2015 15:23:39 GMTP3P:policyref="http://www.w3.org/2014/08/p3p.xml"Server:Apache/2TCN:choiceTransfer-Encoding:chunkedVary:negotiate,accept-language

<html><p>hello World</p></html>

Page 27: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Response packet

HTTP/1.1 200 OKContent-Language:enContent-Location:O-HTTP-charset.en.phpContent-Type:text/html; charset=UTF-8Date:Tue, 19 May 2015 15:23:39 GMTP3P:policyref="http://www.w3.org/2014/08/p3p.xml"Server:Apache/2TCN:choiceTransfer-Encoding:chunkedVary:negotiate,accept-language

<html><p>hello World</p></html>

Page 28: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Packet 구조 Response packet

HTTP/1.1 200 OKContent-Language:enContent-Location:O-HTTP-charset.en.phpContent-Type:text/html; charset=UTF-8Date:Tue, 19 May 2015 15:23:39 GMTP3P:policyref="http://www.w3.org/2014/08/p3p.xml"Server:Apache/2TCN:choiceTransfer-Encoding:chunkedVary:negotiate,accept-language

<html><p>hello World</p></html>

Page 29: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Response packet

HTTP/1.1 200 OKStatus codeVersion

Page 30: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code

For example

Page 31: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code

100 200 300 400 500

Page 32: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code Informational 1xx

100 Continue서버는 이 코드를 제공하여 요청의첫 번째 부분을 받았으며 나머지를 기다리고 있음을 나타낸다 .

101 Switching Protocols서버에 프로토콜 전환을 요청했으며서버는 이를 승인하는 중이다 .

Page 33: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code 200 OK이는 주로 서버가 요청한 페이지를 제공했다는 의미로 쓰인다 .

201 Created서버가 새 리소스를 작성했다 .

202 Accepted접수했지만 아직 처리하지 않았다 .

203 Non-Authoritative Information다른 소스에서 수신된 정보를 제공하고 있다 .

204 No Content처리했지만 콘텐츠를 제공하지 않는다 .

Successful 2xx

Page 34: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code

300 Multiple Choices서버가 사용자 에이전트에 따라 수행할 작업을 선택하거나 ,요청자가 선택할 수 있는 작업 목록을 제공한다 .

301 Moved PermanentlyGET 또는 HEAD 요청에 대한 응답으로이 응답을 표시하면 요청자가 자동으로 새 위치로 전달된다 .

302 Found요청자는 향후 요청 시 원래 위치를 계속 사용해야 한다 .

304 Not Modified마지막 요청 이후 요청한 페이지는 수정되지 않았다 .

307 Temporary Redirect현재 서버가 다른 위치의 페이지로 요청에응답하고 있지만 요청자는 향후 요청 시원래 위치를 계속 사용해야 한다 .

Redirection 3xx

Page 35: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Status Code

400 Bad Request서버가 요청의 구문을 인식하지 못했다 .

401 Unauthorized서버는 로그인이 필요한 페이지에대해 이 요청을 제공할 수 있다 .

404 Not Found서버가 요청한 페이지를 찾을 수 없다 .

405 Method Not Allowed요청에 지정된 방법을 사용할 수 없다 .

408 Request Timeout서버의 요청 대기가 시간을 초과하였다 .

413 Request Entity Too Large요청이 너무 커서 서버가 처리할 수 없다 .

Client Error 4xx

Page 36: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Server Error 5xxStatus Code

500 Internal Server Error서버에 오류가 발생하여 요청을 수행할 수 없다 .

501 Not Implemented서버에 요청을 수행할 수 있는 기능이 없다 .

502 Bad Gateway서버가 게이트웨이나 프록시일때 ,업스트림 서버에서 잘못된 응답을 받았다 .

503 Service Unavailable서버가 오버로드되었거나 유지관리를 위해 다운됨 .

504 Gateway Timeout서버가 게이트웨이나 프록시일때 ,응답을 제때 못 받았다 .

505 HTTP Version Not Supported사용된 HTTP 프로토콜 버전을 지원안함 .

Page 37: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Header

Page 38: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HeaderAcceptAccept-CharsetAccept-FeaturesAccept-EncodingAccept-LanguageAccept-RangesAccess-Control-Allow-CredentialsAccess-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-HeadersAccess-Control-Max-AgeAccess-Control-Expose-HeadersAccess-Control-Request-MethodAccess-Control-Request-Headers

If-None-MatchIf-RangeIf-Unmodified-SinceLast-Event-IDLast-ModifiedLinkLocationMax-ForwardsNegotiateOriginPragmaProxy-AuthenticateProxy-AuthorizationRangeRefererRetry-AfterSec-Websocket-ExtensionsSec-Websocket-KeySec-Websocket-OriginSec-Websocket-ProtocolSec-Websocket-Version

ServerSet-CookieSet-Cookie2Strict-Transport-SecurityTCNTETransfer-EncodingUpgradeUser-AgentVariant-VaryVaryViaWarningWWW-AuthenticateX-Content-DurationX-Content-Security-PolicyX-DNSPrefetch-ControlX-Frame-OptionsX-Requested-With

AgeAllowAlternatesAuthorizationCache-ControlConnectionContent-EncodingContent-Language

Content-LengthContent-LocationContent-MD5Content-RangeContent-Security-PolicyContent-TypeCookieDate

ETagExpectExpiresFromHostIf-Match

Page 39: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HeaderAcceptAccept-CharsetAccept-FeaturesAccept-EncodingAccept-LanguageAccept-RangesAccess-Control-Allow-CredentialsAccess-Control-Allow-OriginAccess-Control-Allow-MethodsAccess-Control-Allow-HeadersAccess-Control-Max-AgeAccess-Control-Expose-HeadersAccess-Control-Request-MethodAccess-Control-Request-Headers

If-None-MatchIf-RangeIf-Unmodified-SinceLast-Event-IDLast-ModifiedLinkLocationMax-ForwardsNegotiateOriginPragmaProxy-AuthenticateProxy-AuthorizationRangeRefererRetry-AfterSec-Websocket-ExtensionsSec-Websocket-KeySec-Websocket-OriginSec-Websocket-ProtocolSec-Websocket-Version

ServerSet-CookieSet-Cookie2Strict-Transport-SecurityTCNTETransfer-EncodingUpgradeUser-AgentVariant-VaryVaryViaWarningWWW-AuthenticateX-Content-DurationX-Content-Security-PolicyX-DNSPrefetch-ControlX-Frame-OptionsX-Requested-With

AgeAllowAlternatesAuthorizationCache-ControlConnectionContent-EncodingContent-Language

Content-LengthContent-LocationContent-MD5Content-RangeContent-Security-PolicyContent-TypeCookieDate

ETagExpectExpiresFromHostIf-Match

Page 40: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Server : 어떤 서버 프로그램의 정보를 클라이언트에게 제공

Host : 요청하는 도메인을 제공ex) Server: nginx/1.7

ex) Host: www.example.comLocation : Moved 일 경우 이동할 주소를 제공

ex) Location: www.example.com

ex) Date: Wed, 15 Nov 1995 06:25:24 GMTDate : 서버의 시간을 제공

Page 41: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

클라이언트가 서버에게 받을 수 있는 파일의 형식 정의

Page 42: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

MIME TYPE

Page 43: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

format;quality=0.90.0 ~ 1.0

Page 44: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Accept-Charset: iso-8859-5, unicode-1-1;q=0.8

ASCII, EUC-KR, EUC-JP, CP949, UTF-8…

Page 45: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

ISO 639-1 Code: ko-kr, en-us, en-gb, ja-jp, zh-cn, zh-tw…

Accept-Language: ko-kr,ko;q=0.8,en-us;q=0.5,en;q=0.3

클라이언트가 서버에게 받을 수 있는 언어의 종류를 보냄

Page 46: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Accept-Encoding: gzip, deflate, sdch

클라이언트가 서버에게 가능한 자신이 해석 가능한 압축방법을 제시함

Page 47: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header Accept-Encoding: gzip, deflate, sdch

클라이언트가 서버에게 가능한 자신이 해석 가능한 압축방법을 제시함compress – UNIX "compress" program methoddeflate – compression based on the deflate algorithm, wrapped inside the zlib data formatexi – W3C Efficient XML Interchangegzip – GNU zip format. This method is the most broadly supportedidentity – No transformation is used. This is the default value for content coding.pack200-gzip – Network Transfer Format for Java Archives.bzip2 – compression based on the free bzip2 format, supported by lighttpdlzma – compression based on (raw) LZMA is available in Opera 20, and in elinks via a compile-time optionpeerdist – Microsoft Peer Content Caching and Retrievalsdch – Google Shared Dictionary Compression for HTTP, based on VCDIFF.xz - LZMA2-based content compression, supported by a non-official Firefox patch.

Page 48: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Content-Encoding: gzip

서버가 클라이언트에게 보내는 내용의 압축방식을 알려줌

Page 49: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Content-Encoding: gzip

서버가 클라이언트에게 보내는 내용의 압축방식을 알려줌

Page 50: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Content-Type: text/html; charset=utf-8

서버가 클라이언트에게 보내는 내용의 type 을 알려줌

Page 51: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Header

Content-Length: 23021

보내는 내용의 크기를 바이트 단위로 알려줌

Page 52: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HTTP 2

HTTP 2

Page 53: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HTTP 2

엄청난 진보= 전체 페이지 사이즈 증가

In 1998 google

Now google

Page 54: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HTTP 2

Page 55: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HTTP 2

• 훨씬 더 많은 리소스로 구성되어 있다 .

• 다수의 도메인을 사용한다 .

• 과거에 비해 매우 동적으로 동작한다 .

• 보안이 보다 중요한 이슈가 되었다 .

• 헤더의 길이가 길어졌다 .

Page 56: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

HTTP 2

HPACK / Binary Protocol Server Push

Multiplexing

HTTP 2 장점

Page 57: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

WSGI

WSGI

PEP 333 / PEP 3333

WebApplication

HandlerClient

Page 58: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Examples

Page 59: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Examples

https://github.com/Luavis/GDG-Devcamp-2016

https://git.io/v61jO

or

Page 60: GDG Dev camp 발표자료 - python으로 만들어보는 http서버

Referenceshttp://nuli.navercorp.com/sharing/blog/post/1132452https://www.maxcdn.com/blog/spdy-http2-shift/http://cacm.acm.org/magazines/2012/12/157870-spdying-up-the-web/abstracthttp://www.slideshare.net/oddpoet/spdy-13231459http://helloworld.naver.com/http://www.w3.org/http://wikipedia.org/http://wpcertification.blogspot.kr/2010/07/difference-between-no-cache-and-no.htmlhttp://icecreamie.tistory.com/52http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/

Thanks!감사합니다 !http://fb.com/luavis

http://github.com/Luavishttp://luavis.kr