python requests.get方法

时间:2017-07-22 03:59:45

标签: python python-3.x python-requests configuration-files http-error

哦,兄弟!过去两个小时我一直在摸不着头脑。最后,我决定将其转换为stackoverflow上的强大功能。你们是蟒蛇战士,请帮忙

这有效:

requests.get('https://abc.123.xyz.xom/getmethisurl',
             auth=('PasswordIsAuthToken', 'khcdhk-dcbdmsb-dcbdsm-aBSDCXKN'),
             verify=False)

这并不是每次都获得401:

defconn.py

import json
import requests
from requests.auth import HTTPBasicAuth


class connect(object):

    def __init__(self, url, user, token):
        self.url=url
        self.user=user
        self.token=token

    def uget(self, uri):
        self.url = self.url + uri

        ## EDIT 1 : Code added to print out the parameters values and type for debugging
        print ("IN:", repr(self.url), repr(self.user), repr(self.token))
        print(self.url, type(self.url))
        print(self.user, type(self.user))
        print(self.token, type(self.token))

        res=requests.get(self.url, auth=(self.user, self.token), verify=False)
        if res.status_code == 401:
           print ("ERORR 401 !!!!")
        else:
           return res

app.py(这是主脚本)

import defconn
from connexion.resolver import RestyResolver

props = dict(line.strip().split('=') for line in open('env.properties'))

url=props['connect.url']
user=props['connect.username']
token=props['connect.token']

oj=defconn.connect(url,user,token)
oj.uget('/getmethisurl')

这是我的env.properties:

env.properties

connect.url=https://abc.123.xyz.xom
connect.username=PasswordIsAuthToken
connect.token=khcdhk-dcbdmsb-dcbdsm-aBSDCXKN

输出

output

非常感谢任何帮助。

编辑1:添加诊断代码及其输出屏幕截图

谢谢 - A

1 个答案:

答案 0 :(得分:0)

可能的原因是参数的某个地方与简单的硬编码示例不匹配。尝试添加诊断线,以便隔离问题:

class connect(object):

    def __init__(self, url, user, token):
        self.url=url
        self.user=user
        self.token=token

    def uget(self, uri):
        self.url = self.url + uri
        print(repr(self.url), repr(self.user), repr(self.token)) # <== DIAGNOSTIC
        res=requests.get(self.url, auth=(self.user, self.token), verify=False)
        if res.status_code == 401:
           print ("ERORR 401 !!!!")
        else:
           return res

正如评论中所述,self.url = self.url + uri可能并不是您想要的。相反,只需更新一个局部变量。