ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] request 모듈 간단 사용법
    언어/파이썬 & 장고 2016. 12. 1. 14:06

    Python에서 HTTP 요청을 보내는 모듈인 requests를 간단하게 정리해보겠습니다.

    HTTP GET 요청 방법

    import requests
     
    url = 'http://google.com'
    response = requests.get(url=url)
    print(response.status_code)
    print(response.text)
     
    # 결과
    # 200
    # <!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="ko"><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>Google</title><script>(function(){window.google={kEI:'Hqo_WM3MLIK20QT3wIcg',kEXPI:'1351903,1352298,3700062,3700299,4026240,4029815,4031109,4032678,4036527,4038012,4039268,4043492,4045841,4048347,4052304,4065787,4065918,4068550,4068816,4069839,4069841,4070138,4071842,4072364,4072602,4072777,4073405,4073728,4073757,4073959,4074348,4076096,4076316,4076931,4076998,4078430,4078438,4078457,4078762,4079105,4079625,4079894,4080164,4080271,4080629,4081037,4081039,4081265,4081273,4081425,4082217,4082619,4083030,4083476,4083517,4084147,4084296,4084298,4084343,4084348,4084956,4085628,4085769,4086062,4086134,4086199,4086708,4086874,4087170,4087182,4087946,4087967,4088364,4088643,4089027,4089201,8300096,8300272,8502184,8503585,8504846,8505025,8505259,8505275,8505831,8506340,8506399,8506478,8506615,8507191,8507295,8507380,8507420,8507858,8507860,8507897,8507899,8507911,8507951,8507974,8508022,8508059,8508066,8508229,8508292,8508314,8508351,8508395,8508421,8508992,8509016,10200083,10202194,13500019',authuser:0,kscs:'c9c918f0_24'};google.kHL='ko';})();(function(){google.lc=[];google.li=0;google.getEI=function(a){for(var b;a&&(!a.getAttribute||!(b=a.getAttribute("eid")));)a=a.parentNode;return b||google.kEI};google.getLEI=function(a){for(var b=null;a&&(!a.getAttribute||!(b=a.getAttribute("leid")));)a=a.parentNode;return b};google.https=function(){return"https:"==window.location.protocol};google.ml=function(){return null};google.wl=function(a,b){try{google.ml(Error(a),!1,b)}catch(c){}};google.time=function(){return(new Date).getTime()};google.log=function(a,b,c,e,g){a=google.logUrl(a,b,c,e,g);if(""!=a){b=new Image;var d=google.lc,f=google.li;d[f]=b;b.onerror=b.onload=b.onabort=function(){delete d[f]};window.google&&window.google.vel&&window.google.vel.lu&&window.google.vel.lu(a);b.src=a;google.li=f+1}};google.logUrl=function(a,b,c,e,g){var d="",f=google.ls||"";if(!c&&-1==b.search("&ei=")){var h=google.getEI(e),d="&ei="+h;-1==b.search("&lei=")&&((e=google.getLEI(e))?d+="&lei="+e:h!=google.kEI&&(d+="&lei="+google.kEI))}a=c||"/"+(g||"gen_204")+"?atyp=i&ct="+a+"&cad="+b+d+f+"&zx="+google.time();/^http:/i.test(a)&&google.https()&&(google.ml(Error("a"),!1,{src:a,glmm:1}),a="");return a};google.y={};google.x=function(a,b){google.y[a.id]=[a,b];return!1};google.lq=[];google.load=function(a,b,c){google.lq.push([[a],b,c])};google.loadAll=function(a,b){google.lq.push([a,b])};})();var a=window.location,b=a.href.indexOf("#");if(0<=b){var c=a.href.substring(b+1);/(^|&)q=/.test(c)&&-1==c.indexOf("#")&&a.replace("/search?"+c.replace(/(^|&)fp=[^&]*/g,"")+"&cad=h")};</script><style>#gbar,#guser{font-size:13px;padding-top:1px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}
    # </style><style>body,td,a,p,.h{font-family:굴림,돋움,arial,sans-serif}.ko{font-size:9pt}body{margin:0;overflow-y:scroll}#gog{padding:3px 8px 0}td{line-height:.8em}.gac_m td{line-height:17px}form{margin-bottom:20px}.h{color:#36c}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}em{font-weight:bold;font-style:normal}.lst{height:25px;width:496px}.gsfi,.lst{font:18px arial,sans-serif}.gsfs{font:17px arial,sans-serif}.ds{display:inline-box;display:inline-block;margin:3px 0 4px;margin-left:4px}input{font-family:inherit}a.gb1,a.gb2,a.gb3,a.gb4{color:#11c !important}body{background:#fff;color:black}a{color:#11c;text-decoration:none}a:hover,a:active{text-decoration:underline}.fl a{color:#36c}a:visited{color:#551a8b}a.gb1,a.gb4{text-decoration:underline}a.gb3:hover{text-decoration:none}#ghead a.gb2:hover{color:#fff !important}.sblc{padding-top:5px}.sblc a{display:block;margin:2px 0;margin-left:13px;font-size:11px}.lsbb{background:#eee;border:solid 1px;border-color:#ccc #999 #999 #ccc;height:30px}.lsbb{display:block}.ftl,#fll a{display:inline-block;margin:0 12px}.lsb{background:url(/images/nav_logo229.png) 0 -261px repeat-x;border:none;color:#000;cursor:pointer;height:30px;margin:0;outline:0;font:15px arial,sans-serif;vertical-align:top}.lsb:active{background:#ccc}.lst:focus{outline:none}.tiah{width:458px}</style><script></script><link href="/images/branding/product/ico/googleg_lodp.ico" rel="shortcut icon"></head><body bgcolor="#fff"><script>(function(){var src='/images/nav_logo229.png';var iesg=false;document.body.onload = function(){window.n && window.n();if (document.images){new Image().src=src;}
    # if (!iesg){document.f&&document.f.q.focus();document.gbqf&&document.gbqf.q.focus();}
    # }
    # })();</script><div id="mngb"> <div id=gbar><nobr><b class="gb1">검색</b> <a class="gb1" href="http://www.google.co.kr/imghp?hl=ko&tab=wi">이미지</a> <a class="gb1" href="http://maps.google.co.kr/maps?hl=ko&tab=wl">지도</a> <a class="gb1" href="https://play.google.com/?hl=ko&tab=w8">Play</a> <a class="gb1" href="http://www.youtube.com/?gl=KR&tab=w1">YouTube</a> <a class="gb1" href="http://news.google.co.kr/nwshp?hl=ko&tab=wn">뉴스</a> <a class="gb1" href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class="gb1" href="https://drive.google.com/?tab=wo">드라이브</a> <a class="gb1" style="text-decoration:none" href="https://www.google.co.kr/intl/ko/options/"><u>더보기</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class="gbi"></span><span id=gbf class="gbf"></span><span id=gbe></span><a href="http://www.google.co.kr/history/optout?hl=ko" class="gb4">웹 기록</a> | <a  href="/preferences?hl=ko" class="gb4">설정</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=ko&passive=true&continue=http://www.google.co.kr/%3Fgfe_rd%3Dcr%26ei%3DHqo_WLGBIuzc8gfSwYQQ" class="gb4">로그인</a></nobr></div><div class="gbh" style=left:0></div><div class="gbh" style=right:0></div> </div><center><br clear="all" id="lgpd"><div id="lga"><div style="padding:28px 0 3px"><div style="height:110px;width:276px;background:url(/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png) no-repeat" title="Google" align="left" id="hplogo" onload="window.lol&&lol()"><div style="color:#777;font-size:16px;font-weight:bold;position:relative;top:70px;left:218px" nowrap="">한국
    # </div></div></div><br></div><form action="/search" name="f"><table cellpadding="0" cellspacing="0"><tr valign="top"><td width="25%">&nbsp;</td><td align="center" nowrap=""><input name="ie" value="EUC-KR" type="hidden"><input value="ko" name="hl" type="hidden"><input name="source" type="hidden" value="hp"><input name="biw" type="hidden"><input name="bih" type="hidden"><div class="ds" style="height:32px;margin:4px 0"><div style="position:relative;zoom:1"><input style="color:#000;margin:0;padding:5px 8px 0 6px;vertical-align:top;padding-right:38px" autocomplete="off" class="lst tiah" value="" title="Google 검색" maxlength="2048" name="q" size="57"><img src="/textinputassistant/tia.png" style="position:absolute;cursor:pointer;right:5px;top:4px;z-index:300" onclick="(function(){var src='/textinputassistant/11/ko_tia.js';var s=document.createElement('script');s.src=src;google.dom.append(s);})();" alt="" height="23" width="27"></div></div><br style="line-height:0"><span class="ds"><span class="lsbb"><input class="lsb" value="Google 검색" name="btnG" type="submit"></span></span><span class="ds"><span class="lsbb"><input class="lsb" value="I’m Feeling Lucky" name="btnI" onclick="if(this.form.q.value)this.checked=1; else top.location='/doodles/'" type="submit"></span></span></td><td class="fl sblc" align="left" nowrap="" width="25%"><a href="/advanced_search?hl=ko&amp;authuser=0">고급검색</a><a href="/language_tools?hl=ko&amp;authuser=0">언어도구</a></td></tr></table><input id="gbv" name="gbv" type="hidden" value="1"></form><div id="gac_scont"></div><div style="font-size:83%;min-height:3.5em"><br></div><span id="footer"><div style="font-size:10pt"><div style="margin:19px auto;text-align:center" id="fll"><a href="/intl/ko/ads/">광고 프로그램</a><a href="http://www.google.co.kr/intl/ko/services/">비즈니스 솔루션</a><a href="https://plus.google.com/102197601262446632410" rel="publisher">+Google</a><a href="/intl/ko/about.html">Google 정보</a><a href="http://www.google.co.kr/setprefdomain?prefdom=US&amp;sig=__4qjt0eNYfMZm-khcNn0lC9L68Fo%3D" id="fehl">Google.com</a></div></div><p style="color:#767676;font-size:8pt">&copy; 2016 - <a href="/intl/ko/policies/privacy/">개인정보처리방침</a> - <a href="/intl/ko/policies/terms/">약관</a></p></span></center><script>(function(){window.google.cdo={height:0,width:0};(function(){var a=window.innerWidth,b=window.innerHeight;if(!a||!b)var c=window.document,d="CSS1Compat"==c.compatMode?c.documentElement:c.body,a=d.clientWidth,b=d.clientHeight;a&&b&&(a!=google.cdo.width||b!=google.cdo.height)&&google.log("","","/client_204?&atyp=i&biw="+a+"&bih="+b+"&ei="+google.kEI);})();})();</script><div id="xjsd"></div><div id="xjsi"><script>(function(){function c(b){window.setTimeout(function(){var a=document.createElement("script");a.src=b;document.getElementById("xjsd").appendChild(a)},0)}google.dljp=function(b,a){google.xjsu=b;c(a)};google.dlj=c;})();(function(){window.google.xjsrm=[];})();if(google.y)google.y.first=[];if(!google.xjs){window._=window._||{};window._._DumpException=function(e){throw e};if(google.timers&&google.timers.load.t){google.timers.load.t.xjsls=new Date().getTime();}google.dljp('/xjs/_/js/k\x3dxjs.hp.en_US.Q9axaIts5S8.O/m\x3dsb_he,d/rt\x3dj/d\x3d1/t\x3dzcms/rs\x3dACT90oHPA9m23E5thYqPZbPr95c1Fz_BZw','/xjs/_/js/k\x3dxjs.hp.en_US.Q9axaIts5S8.O/m\x3dsb_he,d/rt\x3dj/d\x3d1/t\x3dzcms/rs\x3dACT90oHPA9m23E5thYqPZbPr95c1Fz_BZw');google.xjs=1;}google.pmc={"sb_he":{"agen":true,"cgen":true,"client":"heirloom-hp","dh":true,"dhqt":true,"ds":"","fl":true,"host":"google.co.kr","isbh":28,"jam":0,"jsonp":true,"msgs":{"cibl":"검색어 지우기","dym":"이것을 찾으셨나요?","lcky":"I’m Feeling Lucky","lml":"자세히 알아보기","oskt":"입력 도구","psrc":"검색어가 \u003Ca href=\"/history\"\u003E웹 기록\u003C/a\u003E에서 삭제되었습니다.","psrl":"삭제","sbit":"이미지로 검색","srch":"Google 검색"},"nds":true,"ovr":{},"pq":"","refpd":true,"refspre":true,"rfs":[],"scd":10,"sce":5,"stok":"joWgkkfFG8UgMr4F3vOT2FjLyes"},"d":{}};google.y.first.push(function(){if(google.med){google.med('init');google.initHistory();google.med('history');}});if(google.j&&google.j.en&&google.j.xi){window.setTimeout(google.j.xi,0);}
    # </script></div></body></html>

    www.google.com주소로 GET 요청을 하고 서버에서는 해당 요청을 받아 작업을 처리한 후 요청자인 사용자에게 응답을 줍니다. 응답이 정상적이기 때문에 상태코드 200과 응답내용인 HTML코드를 같이 받았습니다.


    만약 GET API를 호출하고 결과를 JSON으로 받고자 하면 아래와 같이 사용합니다.

    import requests
     
    url = 'http://localhost/test'
    response = requests.get(url=url)
    print(response.json())
     
    # 결과
    # {'message': 'Success.', 'status': 200, 'data': None}

    Parameter 전달법

    com_code = '1234'
    params = {'code': code}
    r = requests.get(url='http://localhost/test', params=params)
    print(r.json())
    print(r.url)
    
    # 결과
    # {'message': 'Success.', 'status': 200, 'data': None}
    # http://localhost/test?code=1234

    인자값으로 parameter를 던지지 않고 url에 덕지덕지 붙여서 보낼 순 있으나 requests모듈에서 기능을 제공해주니 깔끔하게 위와 같이 사용하는게 이쁩니다.

    HTTP POST 요청 방법

    Parameter 전달법

    post의 경우 대개 바디에 값을 추가하고 전달하기 때문에 요청방법과 parameter 추가를 같이 설명하겠습니다.

    import requests
     
    com_code = '1234'
    params = {'code': code}
    url = 'http://localhost/test'
    response = requests.post(url=url, data=params)
    print(response.json())

    get과 차이점은 get에서는 params로 받도록 되어 있지만 post는 data로 받도록 되어있습니다.


    조금 더 복잡한 구조로 POST 요청을 해야 할 때가 있습니다. 전달하는 데이터를 dict타입이 아닌 str타입으로 전달해야될 때가 있는데 json 모듈을 사용하면 쉽게 변환할 수 있습니다.

    import requests, json
     
    com_code = '1234'
    params = {'code': code}
    url = 'http://localhost/test'
    response = requests.post(url=url, data=json.dumps(params))
    print(response.json())

    HEADER, COOKIE 추가

    별도의 헤더 옵션을 추가하고자 할 때는 headers 옵션을, 쿠키를 추가하여 요청을 보내고 싶으면 cookies 옵션을 사용하면 됩니다.

    headers = {'Content-Type': 'application/json; charset=utf-8'}
    cookies = {'id': 'WJEJGJ9231M1RR2OP'}
    res = requests.get(URL, headers=headers, cookies=cookies)

    TIMEOUT 제어

    requests로 get이나 post를 요청한 다음 응답을 기다릴 때, timeout 시간을 조절할 수 있습니다.

    import requests
     
    url = 'http://localhost/test'
    response = requests.get(url=url, timeout=1) # 1초 뒤 timeout exception 발생
    print(response.json())

    응답

    요청의 결과로 받는 응답 객체는 많은 정보와 기능을 가지고 있습니다. 여기서는 자주 사용할 것 같은 기능 몇 가지만 설명하겠습니다.

    response.request # 내가 보낸 request 객체에 접근 가능
    response.status_code # 응답 코드
    response.raise_for_status() # 200 OK 코드가 아닌 경우 에러 발동
    response.json() # json response일 경우 딕셔너리 타입으로 바로 변환
    response.url # 요청 url 확인
    response.text # 응답을 text형식으로 출력


    댓글