如何重置请求?

时间:2015-11-11 22:47:07

标签: python python-requests

我正在尝试为封闭的API编写REST客户端。我两次调用一个特定的函数,但它只在第一次工作。这是同一个命令。如果我用批处理运行脚本两次它工作。我怀疑请求保持连接活动或缓存数据。我如何“重置”请求?

base_headers = {"Connection":"keep-alive",
            "User-Agent":user_agent,
            "Accept-Encoding":"gzip",
            "Host":"xxxxxxxxxxx",
            "Content-Type":"application/json; charset=UTF-8"}
global auth
auth = 'xxxxxx'


def create_header(doAuth = True):
    headers = base_headers
    if doAuth:
       headers['Authorization'] = auth
    return headers
def do_get(url):
    return requests.get(url, headers=create_header());


def do_put(url, payload):
    if payload is None:
        return requests.put(url, headers=create_header())

    return requests.put(url, data=payload, headers=create_header())


def upvote(postid):
    return do_put("xxxxxxxxxxxxx" % postid, None).content


def set_pos(longtitude, latitude, place, country="DE"):
    payload = {'xxxxx':'xxxx'}

    json_payload = json.dumps(payload)

    return do_put("xxxxxxxxxxxxxxx",json_payload).content


def do_post(url, payload, doAuth=True):
    return requests.post(url, data=payload, headers=create_header(doAuth=doAuth))

def new_acc(place, latitude, longtitude):
    access = get_access_token(place, latitude, longtitude)
    print access
    global auth
    auth = "Bearer %s" % access['access_token']
    set_pos(longtitude, latitude, place) # Does a POST

for i in range(1,30):
    posts = new_acc(uni['city'], uni['latitude'], uni['longtitude'])
    upvote('xxxxxxxxxxxxx')

基本上第一个upvote()每次都会经历,但每次成功都没有。

1 个答案:

答案 0 :(得分:1)

我几乎肯定keep-alive没有这样做。如果在请求之后没有得到预期的响应代码,我建议编写一些处理程序来调试响应。

另外,我是否可以建议稍微不同的设计,以避免global

class RESTClient(object):

    BASE_HEADERS = {"Connection":"keep-alive",
                    "Accept-Encoding":"gzip",
                    "Host":"xxxxxxxxxxx",
                    "Content-Type":"application/json; charset=UTF-8"}

    def __init__(self, user_agent, auth = None):
        self.headers = dict(self.BASE_HEADERS)
        self.headers['User-Agent'] = user_agent
        self.auth = auth

    def create_header(self):
        headers = dict(self.headers)
        if auth:
            headers['Authorization'] = auth
        return headers

    def do_get(self, url):
        return requests.get(url, headers=self.create_header());


    def do_put(self, url, payload = None):
        if not payload:
            return requests.put(url, headers=self.create_header())
        return requests.put(url, data=payload, headers=self.create_header())


    def upvote(self, postid):
        return do_put("xxxxxxxxxxxxx" % postid).content

    def set_pos(self, longtitude, latitude, place, country="DE"):
        payload = {'xxxxx':'xxxx'}
        json_payload = json.dumps(payload)
        return do_put("xxxxxxxxxxxxxxx",json_payload).content

    def do_post(self, url, payload, do_auth = None):
        return requests.post(url, data=payload, headers=self.create_header())

    def new_acc(self, place, latitude, longtitude):
        access = get_access_token(place, latitude, longtitude)
        print access
        self.auth = "Bearer %s" % access['access_token']
        set_pos(longtitude, latitude, place)

rc = RESTClient('Mozilla/5.0 ... Firefox/38.0', 'my-auth-token')
rc.upvote(post_id)
etc...

可能是你对全局变量的使用破坏了事情。没有运行你的代码,我无法检查,但我会避免使用global s并支持通过对象更严格地控​​制你的状态。

修改 鉴于该答案被OP接受,我认为缺陷是由全局变异引起的。