GET 방식

import urllib2 # 1. 일반적 GET으로 호출 하는 방법 (url ? query string으로 날릴시) response = urllib2.urlopen('http://test.com?param1=a&param2=b') # 2. header 의 값이 필요 할때 request = urllib2.Request('http://test.com?param1=a&param2=b', None, {'Content-Type': 'application/json'}) response = urllib2.urlopen(request) # 3. 2번과 동일 하나 명시적으로 파라미터를 호출 request = urllib2.Request('http://test.com?param1=a&param2=b', headers={'Content-Type': 'application/json'}) response = urllib2.urlopen(request)


POST 방식

import urllib2
import urllib
# POST 요청은 data 값이 들어 있는가에 따라 요청 방식이 달라진다.
data = '{"param1":"a", "param2" : "b"}'
data = urllib.urlencode(data)
request = urllib2.Request('http://test.com', data, xxxx)
response = urllib2.urlopen(request)


차이점은 urllib2.Request() 함수에 url 인자에 파라미터를 전부 보내느냐  <- GET

2번째 인자로 파라미터 딕셔너리로 보낼 것이냐 차이. <- POST

+ Recent posts