更新成熟的数据库

2024-05-23 23:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须更新一个成熟的数据库,到目前为止,我设法使用GET从某个IP范围检索数据(但只有IP范围不能尝试其他属性,如country)。想知道如何从SQL数据库获取数据并使用PUT和POST(可能使用whoisrestfulapi?)更新数据库

我是新来的。。。所以。。。我完全是个笨蛋。你知道吗

import json

import pprint

def get_ripe(uri):

    try:
        from urlparse import urlparse
    except ImportError:
        from urllib.parse import urlparse

    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=UTF-8'
    }

    #uri = 'http://rest.db.ripe.net/ripe/inetnum/78.159.216.0/21'

    #path = '/ripe/inetnum/78.159.216.0/21'

    target = urlparse(uri)
    method = 'GET'
    body = ''

    h = http.Http()

    # If you need authentication some example:
    #if auth:
        #h.add_credentials(auth.user, auth.password)

    response, content = h.request(
            target.geturl(),
            method,
            body,
            headers)
    return content


while True:

    uri = input('Enter URI: ')#e.g http://rest.db.ripe.net/ripe/inetnum/78.159.216.0/21

    jdata = json.loads(get_ripe(uri))

    print(pprint.pprint(jdata))

Tags: fromimportipauth数据库jsonhttpget