Self-Improvement
python request 모듈 본문
*requests 패키지 설치
pycharm에서 file -> settings -> Project interpreter -> requests 패키지 검색 및 설치
(urllib3, requests, idna, chardet, certifi 가 설치됨)
*requests import
1 | import requests | cs |
*requests 메소드 사용법
1 2 3 4 5 6 7 8 9 | -http://docs.python-requests.org/en/master/user/quickstart/ r=requests.get('https://api.github.com/events') - GET 메소드 r=requests.post('http://httpbin.org/post', data = {'key':'value'}) - POST 메소드 r=requests.put('http://httpbin.org/delete',data = {'key':'value'}) | cs |
*requests payload 사용법
1 2 3 4 | payload = {'key':'value', 'key2':['value2', 'value3']} - key에 value값이 여러개면 리스트로 작성 r=requests.get('http://httpbin.org/get', params=payload) | cs |
*requests response
1 2 3 4 5 6 7 8 | r.headers r.headers.get('content-type') r.text -body r.content r.json() r.raw - raw response content | cs |
*custom headers
1 2 3 | url = 'https://api.github.com/some/endpoint' headers={'user-agent': 'my-app/0.0.1'} r=requests.get(url, headers=headers) | cs |
*json 포맷
1 2 3 | url = 'https://api.github.com/some/endpoint' payload = {'some': 'data'} r = requests.post(url, json=payload) | cs |
*post multipart-encoded file(업로드)
1 2 3 4 5 6 | url = 'http://httpbin.org/post' files = {'file': open('report.xls', 'rb')} files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')} - file 내용을 지정 r = requests.post(url, files=files) r.text | cs |
*response status codes
1 2 3 | r.status_code r.status_code == 200 - 200이면 True 반환 | cs |
*cookies (원세션이 아닌 세션 유지방법)
1 2 3 4 5 6 7 8 9 10 | url='http://httpbin.org/events' data={'username':'admin','password':'password'} proxies='http://localhost:8080' -프록시 설정 s=requests.session() req=request.Reuest('POST', url, data=data) -요청값을 req에 저장 prepared=s.prepare_request(req) -prepared에 쿠키값이 저장됨 resp=s.send(prepared, proxies=proxies) | cs |
*프록시 설정
1 2 | proxies = {'http': 'http://10.10.1.10:8080','https': 'http://10.10.1.10:8080'} requests.get('http://example.org', proxies=proxies) | cs |
*기본 설정방식
1 2 3 4 5 6 7 8 9 | import requests s = requests.session() login_url = 'http://192.168.10.134/dvwa/login.php' login_data = {'username':'admin', 'password':'password', 'Login':'Login'} proxies = {'http':'http://localhost:9000', 'https':'http://localhost:9000'} req = requests.Request('POST', login_url, data=login_data) prepared = s.prepare_request(req) resp = s.send(prepared, proxies = proxies) | cs |
'프로그래밍 > Python' 카테고리의 다른 글
requests 모듈을 통한 DVWA Low Command injection (0) | 2018.10.21 |
---|---|
python BeautifulSoup (0) | 2018.10.21 |
Head First Python 5-1장 (0) | 2018.10.21 |
Head First Python 4장 (0) | 2018.10.21 |
Head First Python 3장 (0) | 2018.10.21 |