Algorithms/Python3
Python으로 json파일 받아오기, https 통신하기
jalynneyoon
2021. 9. 22. 15:34
카카오 2차 코테 준비로 후다닥 정리해보았습니다.
https 통신
import requests
base_url = "base_url넣기"
def post_start(path, data):
headers = {
'Content-Type': 'application/json'
# ... 기타등등 헤더파일 반영
}
full_path = base_url + "/" + path
response = requests.post(full_path, headers=headers, data=data)
return response.text
def get_data(path, auth_key):
headers = {
# ... 기타등등 헤더파일 반영
'Content-Type': 'application/json'
}
full_path = base_url + "/" + path
response = requests.get(full_path, headers=headers)
return response.text
json 형식의 문자열 데이터를 Python객체로 읽기 - json.loads()
import json
json.loads(post_start("start",data_str))
cf ) Json 파일을 읽을 때에는 json.load()함수를 사용한다.
Python 객체를 json 문자열 타입으로 변환하기 - json.dumps()
json_str = json.dumps(cmd_dict) # 딕셔너리 타입을 json string 타입으로 변환