python程序中添加ip識(shí)別提升openai賬號(hào)的安全性
最近封號(hào)風(fēng)波好像波及了很多人,我自己注冊的賬號(hào)都沒有問題的,以前我調(diào)用api都沒有特別在意節(jié)點(diǎn)的問題,賬號(hào)目前還在,我認(rèn)為調(diào)用api不是太大的問題??吹浇裉煊泻芏嗳艘?yàn)楣?jié)點(diǎn)的問題被封號(hào)。我決定給自己的程序添加一個(gè)ip識(shí)別,利用request和geoip模塊提前判斷網(wǎng)絡(luò)狀況和ip地址,防止自己使用不合適的ip來調(diào)用api。
以下是一個(gè)相關(guān)實(shí)例,希望對使用python調(diào)用openai?api的朋友有所幫助。其中
GeoLite2-City.mmdb文件需要自己手動(dòng)下載,下載方式可以問newbing。

import requests
import geoip2.database
# ip地址歸屬列表
# ipaddresslist = ['China','United States','Hong Kong','Singapore','Japan','Australia','Germany','France','South Korea','Taiwan','India', 'Russia', 'Turkey', 'New Zealand']
ipuseopenailist=['United States','Australia','Germany','France','India','Turkey','New Zealand']
ipbanopenailist=['China','Hong Kong','Singapore','Japan','South Korea','Taiwan','Russia']
try:
? ? response = requests.get('https://api.ipify.org')
? ? client_ip = response.text
? ? # print(client_ip)
? ? # Replace YOUR_DATABASE_FILE with the path to your GeoLite2-City.mmdb file
? ? reader = geoip2.database.Reader(r'F:\BaiduNetdiskWorkspace\zahuo\自己的興趣\USEAI\GeoLite2-City\GeoLite2-City.mmdb')
? ? response = reader.city(client_ip)
? ? countryname = response.country.name
? ? reader.close()
? ? if countryname in ipuseopenailist:
? ? ? ? print(countryname,": 可以使用openai")
? ? else:
? ? ? ? print(countryname,": 不可以使用openai")
? ? # print(response.country.name)
? ? # print(response.subdivisions.most_specific.name)
? ? # print(response.city.name)
? ?
except requests.exceptions.RequestException as e:
? ? print("網(wǎng)絡(luò)連接失敗:", e)