有一个注意点,就是代码放在服务器上时,会出现频繁请求数据时,eloqua会重定向url([Errno 104] Connection reset by peer)),这里处理的方法是,添加循环,重新请求,当然请求的次数限制在10次内,如果十次都没有通过,则停止请求(在windows系统上没发现这个问题)
import json
import base64
import requests
import datetime
import time
import psycopg2
import os
import configparser
# 用来操作数据库的类
class GPCommand(object):
# 类的初始化
def __init__(self):
self.hostname = 'xxxxxx'
self.username = 'xxxxxx'
self.password = 'xxxxxxxxxx'
self.database = 'xxxxx'
def connectGp(self):
try:
#链接数据库
#读取配置利用connect链接数据库
self.connect = psycopg2.connect( host=self.hostname, user=self.username, password=self.password, dbname=self.database )
#创建一个新的cursor
self.cursor = self.connect.cursor()
print("connect gp successful.")
return ('con_successful')
except psycopg2.Error:
error = 'Failed to setup Postgres environment.\n{0}'.format(sys.exc_info())
print('connect gp error.'+'\n')
return 'con_error'+ error
#关闭数据库
def closeMysql(self):
self.cursor.close()
self.connect.close()
print("database closed")
def insert_data(self,table_name,dict):
try:
dicts_values = []
# print(dict)
dict_key = list(dict.keys())
table_key = ",".join(dict_key)
ROWstr = '' # 行字段
for key in dict.keys(