기본 HTTP 요청하기
import httplib
conn = httplib.HTTPConnection("httpbin.org")
conn.request("GET", "/ip")
r1 = conn.getresponse()
print r1.status, r1.reason
data1 = r1.read()
print data1
conn.request("GET", "/get")
r2 = conn.getresponse()
print r2.status, r2.reason
data2 = r2.read()
print data2
conn.close()
- https 접속을 하려면, httplib.HTTPConnection 대신에 httplib.HTTPSConnection을 사용한다.
> conn = httplib.HTTPSConnection("httpbin.org")
- 특정 포트 지정시는 아래와 같은 방법 중 한가지를 사용한다.
> conn = httplib.HTTPConnection("httpbin.org:80")
> conn = httplib.HTTPConnection("httpbin.org", 80)
POST 요청하기
import httplib, urllib
conn = httplib.HTTPConnection("httpbin.org")
params = urllib.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn.request("POST", "/post", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
params = "test string"
headers = {"Content-type": "text/html"}
conn.request("POST", "/post", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
참고 사이트
- http://httpbin.org : 각종 HTTP 요청들에 대한 응답을 테스트할 수 있는 사이트
<테스트 환경>
- OS : Windows 7
- Python 버전 : 2.7
'1. 연구 모듈 > Python' 카테고리의 다른 글
| [Python] HTML 파싱하기 (Beautiful Soup 모듈 사용) (0) | 2017.02.01 |
|---|---|
| [Python] HTTP 요청하기 (requests 모듈 사용) (0) | 2016.12.29 |
| [Python] Python에서 패키지 관리 시스템(PIP) 사용하기 (0) | 2016.10.18 |
| [Python 팁] Python에서 한글 사용 (0) | 2015.09.29 |
| [wxPython] Launcher 어플 Template (0) | 2015.09.29 |


