์„œ๋ฒ„์—ฐ๊ฒฐ (ํŒŒ์ด์ฌ)
import pymongo
import certifi

client = pymongo.MongoClient(
    "mongodb+srv://test:sparta@cluster0.hd7sx.mongodb.net/Cluster0?retryWrites=true&w=majority",
    tlsCAFile=certifi.where())
db = client.dbsparta

 

flask๋กœ mongoDB์—ฐ๊ฒฐ + HTML ์—ฐ๊ฒฐ (ํŒŒ์ด์ฌ)
from flask import Flask, render_template, request, jsonify

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route("/homework", methods=["POST"])
def homework_post():
    sample_receive = request.form['sample_give']
    print(sample_receive)
    return jsonify({'msg': 'POST ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

@app.route("/homework", methods=["GET"])
def homework_get():
    return jsonify({'msg': 'GET ์—ฐ๊ฒฐ ์™„๋ฃŒ!'})

if __name__ == '__main__':
    app.run('0.0.0.0', port=5000, debug=True)

 

ํฌ๋กค๋ง(ํŒŒ์ด์ฌ) 
import requests                       #pip install requests
from bs4 import BeautifulSoup         #pip install bs4

@app.route("/movie", methods=["POST"])
def movie_post():
    url_receive = request.form['url_give']
    star_receive = request.form['star_give']
    comment_receive = request.form['comment_give']

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
    data = requests.get(url_receive, headers=headers)

    soup = BeautifulSoup(data.text, 'html.parser')

    title = soup.select_one('meta[property="og:title"]')['content']
    image = soup.select_one('meta[property="og:image"]')['content']
    desc = soup.select_one('meta[property="og:description"]')['content']

    doc = {
        'title': title,
        'image': image,
        'desc': desc,
        'star': star_receive,
        'comment': comment_receive
    }
    db.movies.insert_one(doc)

    return jsonify({'msg':'์ €์žฅ ์™„๋ฃŒ'})

 

html (POST) ์›น๋‚ด์šฉ DB์— ์ €์žฅํ•˜๊ธฐ
function save_comment() {
    let name = $('#name').val()
    let comment = $('#comment').val()

    $.ajax({
        type: 'POST',
        url: '/homework',
        data: {name_give: name, comment_give: comment},
        success: function (response) {
            alert(response['msg'])
            window.location.reload()
        }
    })
}

 

html (GET) DB๋‚ด์šฉ ์›น์— ๋ณด์—ฌ์ฃผ๊ธฐ
function show_comment() {

    $.ajax({
        type: "GET",
        url: "/homework",
        data: {},
        success: function (response) {
            let rows = response['fans']

            for (let i = 0; i < rows.length; i++) {
                let name = rows[i]['name']
                let comment = rows[i]['comment']

                let temp_html = `<div class="card">
                                <div class="card-body">
                                    <blockquote class="blockquote mb-0">
                                        <p>${comment}</p>
                                        <footer class="blockquote-footer">${name}</footer>
                                    </blockquote>
                                </div>
                            </div>`

                $('#comment-list').append(temp_html)
            }
        }
    })
}
mongoDB ์—ฐ๊ฒฐ ์ค‘ ์˜ค๋ฅ˜ (certificate verify fail)

๋งฅ ์‚ฌ์šฉํ•˜์‹œ๋Š” ๋ถ„๋“ค ์ค‘์—์„œ mongoDB ์—ฐ๊ฒฐ ์ค‘ 'certificate verify fail' ํ–ˆ๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋งŽ์•˜๋‹ค. 

๋‚˜๋Š” ์œˆ๋„์šฐ์ธ๋ฐ...?

 

ํ•ด๊ฒฐ

1. certifi๋ฅผ ์„ค์น˜ํ•œ๋‹ค. 

Alt+F12๋ฅผ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์— ํ„ฐ๋ฏธ๋„์ด ๋‚˜์˜จ๋‹ค. 

'pip install certifi' ์ž‘์„ฑํ•˜์—ฌ ์„ค์น˜ํ•œ๋‹ค. 

 

*์„ค์น˜์‹œ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด, ํŒŒ์ผ - ์„ค์ • - ํ”„๋กœ์ ํŠธ - python ์ธํ„ฐํ”„๋ฆฌํ‹ฐ์—์„œ ์„ค์น˜๊ฐ€๋Šฅ

 

2. ์ฝ”๋“œ ์ˆ˜์ • ๋ฐ ์ถ”๊ฐ€ํ•œ๋‹ค.

import pymongo
import certifi

client = pymongo.MongoClient("mongodb+srv://test:sparta@cluster0.hd7sx.mongodb.net/Cluster0?retryWrites=true&w=majority",tlsCAFile=certifi.where())
db = client.dbsparta


doc = {
    'name':'bob',
    'age':27,
}

db.users.insert_one(doc)

 


 

์ถ”๊ฐ€. ํŒŒ์ด์ฌ ์—…๊ทธ๋ ˆ์ด๋“œํ•˜๊ธฐ

1) 'certifi' ์„ค์น˜ ์‹œ, ๋…ธ๋ž‘๊ธ€์”จ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œ๋ฐฉ๋ฒ•์ด ๋‚˜์™€์žˆ๋‹ค๋ฉด,

window + R ๋ˆ„๋ฅด๊ณ . 

'C:  ...  --upgrade pip' ์ฝค๋งˆ ์•ˆ์— ์žˆ๋Š” ๊ธ€์„ ๋ณต์‚ฌ ๋ถ™์ด๋ฉด ์—…๊ทธ๋ ˆ์ด๋“œ ๋.

 


mongoDB ํŒŒ์ด์ฌ์œผ๋กœ ์ž‘๋™ํ•˜๊ธฐ

mongoDB์— ์ €์žฅํ•˜๊ธฐ
doc = {'name':'bobby','age':21}
db.users.insert_one(doc)

 

mongoDB์—์„œ ํ•œ๊ฐœ ์ฐพ๊ธฐ
user = db.users.find_one({'name':'bobby'})

 

mongoDB์—์„œ ์—ฌ๋Ÿฌ๊ฐœ ์ฐพ๊ธฐ ( _id ๊ฐ’์€ ์ œ์™ธํ•˜๊ณ  ์ถœ๋ ฅ)
all_users = list(db.users.find({},{'_id':False}))

 

mongoDB์˜ ๋‚ด์šฉ ๋ฐ”๊พธ๊ธฐ
db.users.update_one({'name':'bobby'},{'$set':{'age':19}})

 

mongoDB์˜ ๋‚ด์šฉ ์ง€์šฐ๊ธฐ
db.users.delete_one({'name':'bobby'})

 

- '์—…๋ฐ์ดํŠธ'๋ฅผ ๋ˆŒ๋ €์„๋•Œ, ์™ธ๋ถ€์˜ ์ž๋ฃŒ๋ฅผ ์ •๋ฆฌํ•ด์„œ ๋ณด์—ฌ์ฃผ๊ธฐ
- ํ•œ๋ฒˆ ๋” '์—…๋ฐ์ดํŠธ'๋ฅผ ๋ˆŒ๋ €์„ ๋•Œ, ์ด์ „์ž๋ฃŒ๋Š” ์‚ญ์ œ๋˜๊ณ  ๋‹ค์‹œ ๋ณด์—ฌ์ฃผ๊ธฐ

 

 

    <script>
        function q1() {
            $('#names-q1').empty()
            $.ajax({
                type: "GET",
                url: "http://spartacodingclub.shop/sparta_api/seoulair",
                data: {},
                success: function (response) {
                    let rows = response["RealtimeCityAir"]["row"]
                    for (let i = 0; i < rows.length; i++) {
                        let gu_name = rows[i]["MSRSTE_NM"]
                        let gu_mise = rows[i]['IDEX_MVL']

                        let temp_html = ``

                        if (gu_mise > 60) {
                            temp_html = `<li class="bad">${gu_name} : ${gu_mise}</li>`
                        } else {
                            temp_html = `<li> ${gu_name} : ${gu_mise}</li>`
                        }

                        $('#names-q1').append(temp_html)
                    }
                }
            })
        }
    </script>

+ Recent posts