__JMY__
MY Devblog
__JMY__
전체 방문자
오늘
어제
  • 분류 전체보기 (52)
    • Dev (52)
      • Algorithm (6)
      • Linux (12)
      • Network (7)
      • Container (2)
      • Python (14)
      • Frontend (2)
      • Etc (9)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Linux
  • algorithm
  • hikaricp
  • Tuple
  • wget
  • flexbox
  • springboot
  • certificate
  • network
  • Kubernetes
  • hash
  • Python
  • Ingress
  • tcpdump
  • flask
  • frontend
  • Sorting
  • SCP
  • Docker
  • react

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
__JMY__

MY Devblog

Dev/Python

[python] REST API 호출

2020. 3. 30. 23:00

REST API 호출

파이썬의 requests, urllib 모듈을 사용하여 REST API 호출하는 방법을 정리하였습니다.

※ requests는 Apache License 2.0의 라이센스를 가진 python HTTP 라이브러리 (requests 공식문서)

※ urllib는 URL 작업을 위한 여러 기능을 가지고 있는 python 라이브러리 (urllib 공식문서)

1. requests 이용 예시

import requests
import json

# GET
res = requests.get('http://127.0.0.1:5000')
print(str(res.status_code) + " | " + res.text)

# POST (JSON)
headers = {'Content-Type': 'application/json; chearset=utf-8'}
data = {'title': 'dummy title', 'id': 1, 'message': 'hello world!'}
res = requests.post('http://127.0.0.1:5000', data=json.dumps(data), headers=headers)
print(str(res.status_code) + " | " + res.text)

2. urllib 이용 예시

from urllib import request, parse
import json

# GET
req = request.Request('http://127.0.0.1:5000')
res = request.urlopen(req)
print(str(res.status) + " | " + res.read().decode('utf-8'))

# POST (JSON)
headers = {'Content-Type': 'application/json; chearset=utf-8'}
data = {'title': 'dummy title', 'id': 1, 'message': 'hello world!'}
req = request.Request('http://127.0.0.1:5000', headers=headers, data=json.dumps(data).encode('utf-8'))
res = request.urlopen(req)
print(str(res.status) + " | " + res.read().decode('utf-8'))
반응형

'Dev > Python' 카테고리의 다른 글

[python] text hash 값 구하기  (0) 2020.04.02
[python] json에서 dictionary로 변환하는 방법  (0) 2020.03.30
[python] Shell 명령어 실행법  (0) 2020.03.30
[python] flask 파일 업로드  (0) 2020.03.03
[python] scp를 이용한 파일전송  (1) 2019.11.27
    'Dev/Python' 카테고리의 다른 글
    • [python] text hash 값 구하기
    • [python] json에서 dictionary로 변환하는 방법
    • [python] Shell 명령어 실행법
    • [python] flask 파일 업로드
    __JMY__
    __JMY__
    공부내용 정리 블로그

    티스토리툴바