呼叫阿里雲介面的python指令碼

2021-08-21 04:52:17 字數 3356 閱讀 2538

# coding=utf-8

import ssl, sys, re

import json

import time

from hdfs import client

# hadoop 集群埠號

hdfs_client = client("")

# 阿里雲秘鑰

ali_code = 'be6b33399ef64216ac409d7d8cf965a6'

# python3

def get_ali(phone, code):

from urllib import request as u_req

host = ''

path = '/mobile'

method = 'get'

# 阿里雲 賬號秘鑰

querys = 'mobile=' + str(phone)

bodys = {}

url = host + path + '?' + querys

request = u_req.request(url)

ctx = ssl.create_default_context()

ctx.check_hostname = false

ctx.verify_mode = ssl.cert_none

response = u_req.urlopen(request, context=ctx)

content = response.read().decode()

if content:

print(content)

return content

# python2

def get_ali_for_py2(phone, code):

import urllib2

host = ''

path = '/mobile'

method = 'get'

# 阿里雲 賬號秘鑰

querys = 'mobile=' + str(phone)

bodys = {}

url = host + path + '?' + querys

request = urllib2.request(url)

ctx = ssl.create_default_context()

ctx.check_hostname = false

ctx.verify_mode = ssl.cert_none

response = urllib2.urlopen(request, context=ctx)

content = response.read()

if content:

print(content)

return content

# 根據路徑返回路徑下的所有檔案path

def make_file_list(path, file_list):

# 判斷python版本

if sys.version_info >= (3, 0):

file_info = hdfs_client.walk(path).__next__()

else:

file_info = hdfs_client.walk(path).next()

for i in file_info[2]:

for e in file_info[1]:

make_file_list(path + '/' + e, file_list)

def get_phone():

paths =

make_file_list(path='/user/hive/warehouse/thematic.db/client_phone_info_temp', file_list=paths)

string = ''

for path in paths:

with hdfs_client.read(path) as f:

ff = f.read().decode()

# print(ff)

for i in ff.split('\n')[:-1]:

print(i)

if sys.version_info >= (3, 0):

res = json.loads(get_ali(i.split(',')[1], ali_code))

else:

res = json.loads(get_ali_for_py2(i.split(',')[1], ali_code))

if res['ret'] != 200:

string += (i.split(',')[0]) + ','

string += (i.split(',')[1]) + ','

string += '未查找到相關資訊' + ','

string += '\n'

continue

string += (i.split(',')[0]) + ','

string += (i.split(',')[1]) + ','

string += res['data']['types'] + ','

string += res['data']['prov'] + ','

string += res['data']['city'] + ','

string += res['data']['isp'] + ','

string += res['data']['area_code'] + ','

string += res['data']['city_code'] + ','

string += res['data']['zip_code'] + ','

string += res['data']['lng'] + ','

string += res['data']['lat'] + ','

string += str(res['data']['num'])

string += '\n'

return string

# get_phone()

def upload_to_hdfs(file_path):

data = get_phone()

hdfs_client.write(file_path, data=data, encoding='utf-8')

if __name__ == '__main__':

rq = time.strftime('%y%m%d%h', time.localtime(time.time()))

file_path = '/phone_message/phone_' + str(rq)

upload_to_hdfs(file_path)

阿里雲人臉識別介面呼叫。

1.阿里雲開通人臉識別 2.以人臉屬性識別為例 3.為了安全起見,我們需要搭建乙個伺服器,以nodejs為例 nodejs var request require request var url require url var crypto require crypto var date new d...

Python介面的實現與呼叫

對於物件導向程式設計來說,有三個概念一定要知道 封裝 繼承和多型。python本身就是一種多型語言,即不同的類中可以定義相同的方法 函式 名稱。所謂介面是一種特殊的類,宣告了若干方法,要求繼承該介面的類必須實現這些方法,否則會報錯。具體 實現如下 from abc import abcmeta,ab...

Python呼叫REST API介面的幾種方式彙總

相信做過自動化運維的同學都用過rest api介面來完成某些動作。api是一套成熟系統所必需的介面,可以被其他系統或指令碼來呼叫,這也是自動化運維的必修課。本文主要介紹python中呼叫rest api的幾種方式,下面是python中會用到的庫。url rac程式設計客棧kid asset impo...