무회blog

python: restFul, REST API, _inputOutput_02, rest,조회,GET,입력,POST, 본문

Python

python: restFul, REST API, _inputOutput_02, rest,조회,GET,입력,POST,

최무회 2020. 8. 25. 08:33
## REST_ FUL 셋팅 
import requests
import json
from bs4 import BeautifulSoup as bs

tt =  input

API_HOST = 'http://localhost:8080/'
_headers = {'Authorization': 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36'}

def req(path, query, method, data={}):
    url = API_HOST + path
    if method == 'GET':
        return requests.get(url, headers=_headers)
    else:
        return requests.post(url, headers=_headers, data=data)

def html_toList(data, sc_num):
    html = data
    html = bs(html,'html.parser')
    title = html.title.text
    print('title: ',title)
    tr = html.body.select('tr')
    x = int(sc_num)
    columns = tr[x].text.split('\n')
    columns = ' '.join(columns).split()
    len(columns)
    return columns


print('success')
## GET, POST 정의 
get_url = 'board'
post_url = 'board/post'

resp = req(get_url, '', 'GET')
resp2 = req(post_url, '', 'POST')   # post_url = 'http://localhost:8080/board/post'

resp.text
dtx = resp.text
print('번호 입력: ')
x = tt()
colums = html_toList(dtx,x)
colums
# post 로 데이터 보내서 DB 에 넣기 
# send Message

print('제목 : ')
subject = tt()
print('작성자 : ')
writer  = tt()
print('내용 : ')
content = tt()

json_va = [subject,writer,content]
def send_data():
    subject = json_va[0]
    writer  = json_va[1]
    content = json_va[2]
    dd = {
         'subject': subject
        ,'writer' : writer
        ,'content': content
    }
    return dd

response = req(post_url,'','post',send_data())

print(response.text)
Comments