我是靠谱客的博主 自信胡萝卜,最近开发中收集的这篇文章主要介绍python 阿里云审核,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#!/usr/bin/python3
# coding=utf-8
import pymssql
import threading
import time
from profile import *
from service import *
import configparser
import decimal
decimal.__version__
cf = configparser.ConfigParser()
cf.read("config.ini")
access_key_id = ""
access_key_secret = ""
profile = Profile(access_key_id, access_key_secret)
api_path_results = r'/green/image/results'
api_path = r'/green/image/asyncscan'
sqlhost=cf.get("Database", "sqlhost")
sqluser=cf.get("Database", "sqluser")
sqlpassword=cf.get("Database", "sqlpassword")
sqldatabase=cf.get("Database", "sqldatabase")
client_info = {
"ip": "127.0.0.1"
}
class myThread (threading.Thread):
def __init__(self,id):
threading.Thread.__init__(self)
self.id=id
def run(self):
while 1:
print(str(self.id)+"开始执行"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
threadLock.acquire()
results=get_date(self.id)
threadLock.release()
if self.id < 5:
ali(results,self.id)
else:
aliresults(results,self.id)
print(str(self.id)+"执行成功"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
def get_date(id):
try:
Alipolitician=0
sqlstr=" update az_ali_photo_20210315 set status="+str(id)+" WHERE
status =0 and ID IN (select top 100 id from az_ali_photo_20210315 with(NOLOCK)
where id>5571335 AND id<11690052
AND Alipolitician=0 and status =0 )"
if id > 5:
Alipolitician=8
sqlstr=" update az_ali_photo_20210315 set status="+str(id)+" WHERE
status =0 and ID IN (select top 100 id from az_ali_photo_20210315 with(NOLOCK)
where id>5571335 AND id<11690052
AND Alipolitician=8 and status =0
and dateadd(MINUTE,-1,getdate())>addtime
)"
conn=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
cur=conn.cursor()
cur.execute(sqlstr)
conn.commit()
cur.close()
conn.close()
conn1=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
cur1=conn1.cursor()
cur1.execute(" select id,taskId,yunpicurl from
az_ali_photo_20210315 with(NOLOCK) WHERE Alipolitician="+str(Alipolitician)+" and status ="+str(id))
results = cur1.fetchall()
cur1.close()
conn1.close()
return results
except Exception as ex:
log("获取数据失败!"+str(ex))
#myThread(id).start()
def ali(results,id):
for row in results:
alicensor(str(row[0]), str(row[2]),id)
def log(logtxt):
with open("log.txt","a") as f:
f.write(logtxt+"n")
def aliresults(results,id):
for row in results:
alicensorresults(str(row[0]), str(row[1]),id)
def alicensor(pid, url,id):
post_data = {
"bizType": "Green",
"scenes": ["ad","terrorism"],
"offline":"true",
"tasks": [
{
"dataId": str(uuid.uuid1()),
"url":url
}
]
}
service = GreenService(profile)
result=""
try:
response = service.send_request(api_path, client_info, post_data,15)
if response=="":
return
#print(response+url)
result = json.loads(response)
if "Request was denied due to user flow control" in str(result):
return
#print(str(result))
if 200 == result["code"]:
taskResults = result["data"]
for taskResult in taskResults:
if(200 == taskResult["code"]):
taskId = taskResult["taskId"]
updateconn=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
updatecur=updateconn.cursor()
#print("update az_ali_photo_20210330 set Alipolitician=8,addtime=getdate(),taskId="+str(taskId)+" WHERE id="+pid)
updatecur.execute("update az_ali_photo_20210315 set Alipolitician=8,status =0,addtime=getdate(),taskId='"+str(taskId)+"' WHERE id="+pid);
updateconn.commit()
updatecur.close()
updateconn.close()
#print('线程'+str(id)+' 检测成功+'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
else:
updateconn=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
updatecur=updateconn.cursor()
updatecur.execute("update az_ali_photo_20210315 set Alipolitician=4,addtime=getdate() WHERE id="+pid);
updateconn.commit()
updatecur.close()
updateconn.close()
else:
updateconn=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
updatecur=updateconn.cursor()
log("检测失败3"+str(pid))
#myThread(id).start()
updatecur.execute("update az_ali_photo_20210315 set Alipolitician=3,addtime=getdate() WHERE id="+pid);
updateconn.commit()
updatecur.close()
updateconn.close()
except Exception as ex:
log("检测失败1!"+str(ex))
time.sleep(3)
#myThread(id).start()
def alicensorresults(pid, taskId,id):
post_data = {
"bizType": "Green",
"scenes": ["ad","terrorism"],
"taskIds": taskId
}
service = GreenService(profile)
result=""
Alipolitician=2
Risktype=0
try:
response = service.send_request(api_path_results, client_info, post_data,10)
if response=="":
return
result = json.loads(response)
#print(str(result))
if "Request was denied due to user flow control" in str(result):
return
if
200 == result["code"]:
taskResults = result["data"]
for taskResult in taskResults:
if(200 == taskResult["code"]):
sceneResults = taskResult["results"]
for sceneResult in sceneResults:
scene = sceneResult["scene"]
label = sceneResult["label"]
suggestion = sceneResult["suggestion"]
if scene=='ad' and label=='customized':
Alipolitician=1
Risktype=1
if scene=='terrorism' and suggestion!='pass':
Alipolitician=1
Risktype=2
except Exception as ex:
log(str(ex)+str(response))
time.sleep(2)
Alipolitician=0
try:
updateconn=pymssql.connect(host=sqlhost,user=sqluser,password=sqlpassword,database=sqldatabase,timeout=30,login_timeout=5)
updatecur=updateconn.cursor()
#print('线程'+str(id)+' 结果+'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
updatecur.execute("update az_ali_photo_20210315 set status =0,Risktype="+str(Risktype)+", Alipolitician="+str(Alipolitician)+",result='"+str(result).replace("'","''")+"' WHERE id="+pid);
updateconn.commit()
updatecur.close()
updateconn.close()
except Exception as ex :
log('检测失败4'+str(ex))
time.sleep(2)
#myThread(id).start()
threadLock = threading.Lock()
if __name__ == '__main__':
try :
myThread(1).start()
time.sleep(10)
myThread(2).start()
time.sleep(10)
myThread(3).start()
#myThread(4).start()
#time.sleep(10)
#myThread(5).start()
#time.sleep(10)
#myThread(6).start()
#time.sleep(10)
#myThread(7).start()
#time.sleep(10)
#myThread(8).start()
#time.sleep(10)
#myThread(9).start()
#time.sleep(10)
#myThread(10).start()
#time.sleep(10)
except Exception as ex :
log(ex)
#!/usr/bin/python3
# coding=utf-8
import uuid
from urllib import parse
from utils import *
import http.client
class GreenService:
def __init__(self, profile):
self.profile = profile
def send_request(self, api_path, client_info, post_data,out_time):
try:
data = json.dumps(post_data)
content_md5 = signature_md5(data.encode("utf-8"))
gmt = gmt_time()
header = {
"Accept": "application/json",
"Content-Type": "application/json",
"Content-MD5": content_md5,
"Date": gmt,
"x-acs-version": "2017-01-12",
"x-acs-signature-nonce": str(uuid.uuid1()),
"x-acs-signature-version": "1.0",
"x-acs-signature-method": "HMAC-SHA1",
}
header["Authorization"] = "acs " + self.profile.access_key_id + ":" + signature_header(header, api_path, client_info, self.profile.access_key_secret)
query = {
"clientInfo": json.dumps(client_info)
}
query_data = parse.urlencode(query).encode('utf-8')
conn = http.client.HTTPConnection("green.cn-shanghai.aliyuncs.com", 80,out_time)
conn.request('POST', api_path + "?%s" % str(query_data, encoding="utf-8"), json.dumps(post_data), header)
response = conn.getresponse()
data = response.read().decode('utf-8')
return data
except Exception as ex:
return ""
#!/usr/bin/python3
# coding=utf-8
class Profile:
def __init__(self, access_key_id, access_key_secret):
self.access_key_id = access_key_id
self.access_key_secret = access_key_secret

最后

以上就是自信胡萝卜为你收集整理的python 阿里云审核的全部内容,希望文章能够帮你解决python 阿里云审核所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(56)

评论列表共有 0 条评论

立即
投稿
返回
顶部