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

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

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

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

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



  • xml_rpc操作数据库,写入过快就会报连接错误
    类似这样:
    [code]
        sock.execute(dbname, uid, pwd, 'stock.seeed.delivery', 'write', id, values)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in call
        return self.__send(self.__name, args)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
        verbose=self.__verbose
      File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
        return self.single_request(host, handler, request_body, verbose)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1289, in single_request
        self.send_request(h, handler, request_body)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1391, in send_request
        connection.putrequest("POST", handler, skip_accept_encoding=True)
      File "/usr/lib/python2.7/httplib.py", line 856, in putrequest
        raise CannotSendRequest()
    CannotSendRequest
    [/code]
    解决这个问题就是每次数据操作前登录,操作后退出,
    让我想到了装饰器,直接上代码
    [code]
    #encoding=utf-8
    import xmlrpclib

    username = 'admin' #the user
    pwd = 'test'      #the password of the user
    dbname = 'test'    #the database
    host = 'http://192.168.1.116:8069'

    def log_in_out(*logs,**kwlogs):
        def deco(func):
            def wrapper(*args,**kwargs):
                dbname,username,pwd,host = kwlogs['dbname'],kwlogs['username'],kwlogs['pwd'],kwlogs['host']
                sock_common = xmlrpclib.ServerProxy ('%s/xmlrpc/common'%host)
                uid = sock_common.login(dbname, username, pwd)
                sock = xmlrpclib.ServerProxy('%s/xmlrpc/object'%host)
                kwargs['sock'] = sock
                kwargs['uid'] = uid
                data = func(*args,**kwargs)
                sock_common.logout(dbname, username, pwd)
                return data
            return wrapper
        return deco

    ##get_list
    @log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)
    def get_list(limit=20000, date_start=None, date_end=None, sock=None, uid=None):
        '''get the list of track'''
        args = [('state','in', ('traceable','partdone','done')), ('transfer_way', 'not like', '%自取%'),
                ('transfer_way', 'not in', ('',)),'|',('track_state','not like','%成功签收%'),('track_state','=',False)]
        if date_start:
            args.insert(0, ('traceable_time', '>=', date_start),)
        if date_end:
            args.insert(0, ('traceable_time', '<=', date_end),)
        ids = sock.execute(dbname, uid, pwd, 'stock.delivery', 'search', args)
        ids = sorted(ids,reverse=True)[:limit]
        print len(ids)
        fields = ['id','logistic_sn','transfer_way','track_state','traceable_time','name'] #fields to read
        data = sock.execute(dbname, uid, pwd, 'stock.delivery', 'read', ids, fields) #ids is a list of id
        return data

    @log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)
    def write_track(id,values,sock=None,uid=None):
        sock.execute(dbname, uid, pwd, 'stock.delivery', 'write', id, values)
    [/code]



  • xml_rpc操作数据库,写入过快就会报连接错误
    类似这样:
    [code]
        sock.execute(dbname, uid, pwd, 'stock.seeed.delivery', 'write', id, values)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1224, in call
        return self.__send(self.__name, args)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1578, in __request
        verbose=self.__verbose
      File "/usr/lib/python2.7/xmlrpclib.py", line 1264, in request
        return self.single_request(host, handler, request_body, verbose)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1289, in single_request
        self.send_request(h, handler, request_body)
      File "/usr/lib/python2.7/xmlrpclib.py", line 1391, in send_request
        connection.putrequest("POST", handler, skip_accept_encoding=True)
      File "/usr/lib/python2.7/httplib.py", line 856, in putrequest
        raise CannotSendRequest()
    CannotSendRequest
    [/code]
    解决这个问题就是每次数据操作前登录,操作后退出,
    让我想到了装饰器,直接上代码
    [code]
    #encoding=utf-8
    import xmlrpclib

    username = 'admin' #the user
    pwd = 'test'      #the password of the user
    dbname = 'test'    #the database
    host = 'http://192.168.1.116:8069'

    def log_in_out(*logs,**kwlogs):
        def deco(func):
            def wrapper(*args,**kwargs):
                dbname,username,pwd,host = kwlogs['dbname'],kwlogs['username'],kwlogs['pwd'],kwlogs['host']
                sock_common = xmlrpclib.ServerProxy ('%s/xmlrpc/common'%host)
                uid = sock_common.login(dbname, username, pwd)
                sock = xmlrpclib.ServerProxy('%s/xmlrpc/object'%host)
                kwargs['sock'] = sock
                kwargs['uid'] = uid
                data = func(*args,**kwargs)
                sock_common.logout(dbname, username, pwd)
                return data
            return wrapper
        return deco

    ##get_list
    @log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)
    def get_list(limit=20000, date_start=None, date_end=None, sock=None, uid=None):
        '''get the list of track'''
        args = [('state','in', ('traceable','partdone','done')), ('transfer_way', 'not like', '%自取%'),
                ('transfer_way', 'not in', ('',)),'|',('track_state','not like','%成功签收%'),('track_state','=',False)]
        if date_start:
            args.insert(0, ('traceable_time', '>=', date_start),)
        if date_end:
            args.insert(0, ('traceable_time', '<=', date_end),)
        ids = sock.execute(dbname, uid, pwd, 'stock.delivery', 'search', args)
        ids = sorted(ids,reverse=True)[:limit]
        print len(ids)
        fields = ['id','logistic_sn','transfer_way','track_state','traceable_time','name'] #fields to read
        data = sock.execute(dbname, uid, pwd, 'stock.delivery', 'read', ids, fields) #ids is a list of id
        return data

    @log_in_out(dbname=dbname,username=username,pwd=pwd,host=host)
    def write_track(id,values,sock=None,uid=None):
        sock.execute(dbname, uid, pwd, 'stock.delivery', 'write', id, values)
    [/code]


登录后回复
 

与 Odoo 中文社区 的连接断开,我们正在尝试重连,请耐心等待