Openerp xml_rpc数据操作过快导致CannotSendRequest的解决方法
-
xml_rpc操作数据库,写入过快就会报连接错误
类似这样:<br /> sock.execute(dbname, uid, pwd, 'stock.seeed.delivery', 'write', id, values)<br /> File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in __call__<br /> return self.__send(self.__name, args)<br /> File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request<br /> verbose=self.__verbose<br /> File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request<br /> return self.single_request(host, handler, request_body, verbose)<br /> File "/usr/lib/python2.7/xmlrpclib.py", line 1289, in single_request<br /> self.send_request(h, handler, request_body)<br /> File "/usr/lib/python2.7/xmlrpclib.py", line 1391, in send_request<br /> connection.putrequest("POST", handler, skip_accept_encoding=True)<br /> File "/usr/lib/python2.7/httplib.py", line 856, in putrequest<br /> raise CannotSendRequest()<br />CannotSendRequest<br />
解决这个问题就是每次数据操作前登录,操作后退出,
让我想到了装饰器,直接上代码<br />#encoding=utf-8<br />import xmlrpclib<br /><br />username = 'admin' #the user<br />pwd = 'test' #the password of the user<br />dbname = 'test' #the database<br />host = 'http://192.168.1.116:8069'<br /><br />def log_in_out(*logs,**kwlogs):<br /> def deco(func):<br /> def wrapper(*args,**kwargs):<br /> dbname,username,pwd,host = kwlogs['dbname'],kwlogs['username'],kwlogs['pwd'],kwlogs['host']<br /> sock_common = xmlrpclib.ServerProxy ('%s/xmlrpc/common'%host)<br /> uid = sock_common.login(dbname, username, pwd)<br /> sock = xmlrpclib.ServerProxy('%s/xmlrpc/object'%host)<br /> kwargs['sock'] = sock<br /> kwargs['uid'] = uid<br /> data = func(*args,**kwargs)<br /> sock_common.logout(dbname, username, pwd)<br /> return data<br /> return wrapper<br /> return deco<br /><br />##get_list<br />@log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)<br />def get_list(limit=20000, date_start=None, date_end=None, sock=None, uid=None):<br /> '''get the list of track'''<br /> args = [('state','in', ('traceable','partdone','done')), ('transfer_way', 'not like', '%自取%'),<br /> ('transfer_way', 'not in', ('',)),'|',('track_state','not like','%成功签收%'),('track_state','=',False)]<br /> if date_start:<br /> args.insert(0, ('traceable_time', '>=', date_start),)<br /> if date_end:<br /> args.insert(0, ('traceable_time', '<=', date_end),)<br /> ids = sock.execute(dbname, uid, pwd, 'stock.delivery', 'search', args)<br /> ids = sorted(ids,reverse=True)[:limit]<br /> print len(ids)<br /> fields = ['id','logistic_sn','transfer_way','track_state','traceable_time','name'] #fields to read<br /> data = sock.execute(dbname, uid, pwd, 'stock.delivery', 'read', ids, fields) #ids is a list of id<br /> return data<br /><br />@log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)<br />def write_track(id,values,sock=None,uid=None):<br /> sock.execute(dbname, uid, pwd, 'stock.delivery', 'write', id, values)<br />