Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Openerp xml_rpc数据操作过快导致CannotSendRequest的解决方法



  • xml_rpc操作数据库,写入过快就会报连接错误
    类似这样:

    <br />&nbsp; &nbsp; sock.execute(dbname, uid, pwd, &#039;stock.seeed.delivery&#039;, &#039;write&#039;, id, values)<br />&nbsp; File &quot;/usr/lib/python2.7/xmlrpclib.py&quot;, line 1224, in __call__<br />&nbsp; &nbsp; return self.__send(self.__name, args)<br />&nbsp; File &quot;/usr/lib/python2.7/xmlrpclib.py&quot;, line 1578, in __request<br />&nbsp; &nbsp; verbose=self.__verbose<br />&nbsp; File &quot;/usr/lib/python2.7/xmlrpclib.py&quot;, line 1264, in request<br />&nbsp; &nbsp; return self.single_request(host, handler, request_body, verbose)<br />&nbsp; File &quot;/usr/lib/python2.7/xmlrpclib.py&quot;, line 1289, in single_request<br />&nbsp; &nbsp; self.send_request(h, handler, request_body)<br />&nbsp; File &quot;/usr/lib/python2.7/xmlrpclib.py&quot;, line 1391, in send_request<br />&nbsp; &nbsp; connection.putrequest(&quot;POST&quot;, handler, skip_accept_encoding=True)<br />&nbsp; File &quot;/usr/lib/python2.7/httplib.py&quot;, line 856, in putrequest<br />&nbsp; &nbsp; raise CannotSendRequest()<br />CannotSendRequest<br />
    


    解决这个问题就是每次数据操作前登录,操作后退出,
    让我想到了装饰器,直接上代码

    <br />#encoding=utf-8<br />import xmlrpclib<br /><br />username = &#039;admin&#039; #the user<br />pwd = &#039;test&#039;&nbsp; &nbsp; &nbsp; #the password of the user<br />dbname = &#039;test&#039;&nbsp; &nbsp; #the database<br />host = &#039;http://192.168.1.116:8069&#039;<br /><br />def log_in_out(*logs,**kwlogs):<br />&nbsp; &nbsp; def deco(func):<br />&nbsp; &nbsp; &nbsp; &nbsp; def wrapper(*args,**kwargs):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dbname,username,pwd,host = kwlogs&#91;&#039;dbname&#039;],kwlogs&#91;&#039;username&#039;],kwlogs&#91;&#039;pwd&#039;],kwlogs&#91;&#039;host&#039;]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sock_common = xmlrpclib.ServerProxy (&#039;%s/xmlrpc/common&#039;%host)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uid = sock_common.login(dbname, username, pwd)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sock = xmlrpclib.ServerProxy(&#039;%s/xmlrpc/object&#039;%host)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kwargs&#91;&#039;sock&#039;] = sock<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; kwargs&#91;&#039;uid&#039;] = uid<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = func(*args,**kwargs)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sock_common.logout(dbname, username, pwd)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return data<br />&nbsp; &nbsp; &nbsp; &nbsp; return wrapper<br />&nbsp; &nbsp; 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 />&nbsp; &nbsp; &#039;&#039;&#039;get the list of track&#039;&#039;&#039;<br />&nbsp; &nbsp; args = [(&#039;state&#039;,&#039;in&#039;, (&#039;traceable&#039;,&#039;partdone&#039;,&#039;done&#039;)), (&#039;transfer_way&#039;, &#039;not like&#039;, &#039;%自取%&#039;),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#039;transfer_way&#039;, &#039;not in&#039;, (&#039;&#039;,)),&#039;|&#039;,(&#039;track_state&#039;,&#039;not like&#039;,&#039;%成功签收%&#039;),(&#039;track_state&#039;,&#039;=&#039;,False)]<br />&nbsp; &nbsp; if date_start:<br />&nbsp; &nbsp; &nbsp; &nbsp; args.insert(0, (&#039;traceable_time&#039;, &#039;&gt;=&#039;, date_start),)<br />&nbsp; &nbsp; if date_end:<br />&nbsp; &nbsp; &nbsp; &nbsp; args.insert(0, (&#039;traceable_time&#039;, &#039;&lt;=&#039;, date_end),)<br />&nbsp; &nbsp; ids = sock.execute(dbname, uid, pwd, &#039;stock.delivery&#039;, &#039;search&#039;, args)<br />&nbsp; &nbsp; ids = sorted(ids,reverse=True)[:limit]<br />&nbsp; &nbsp; print len(ids)<br />&nbsp; &nbsp; fields = &#91;&#039;id&#039;,&#039;logistic_sn&#039;,&#039;transfer_way&#039;,&#039;track_state&#039;,&#039;traceable_time&#039;,&#039;name&#039;] #fields to read<br />&nbsp; &nbsp; data = sock.execute(dbname, uid, pwd, &#039;stock.delivery&#039;, &#039;read&#039;, ids, fields) #ids is a list of id<br />&nbsp; &nbsp; 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 />&nbsp; &nbsp; sock.execute(dbname, uid, pwd, &#039;stock.delivery&#039;, &#039;write&#039;, id, values)<br />