如何在Python中打印返回请求的函数?

时间:2016-10-09 03:31:46

标签: python

我有一个获取用户个人资料数据的函数:

API.py

def getProfileData(self):
    data = json.dumps({
    '_uuid'        : self.uuid,
    '_uid'         : self.username_id,
    '_csrftoken'   : self.token
    })
    return self.SendRequest('accounts/current_user/?edit=true', self.generateSignature(data))

我想在终端中打印返回的请求,所以我这样做了:

test.py

from API import API

API = API("username", "password")
API.login() # login
print(API.getProfileData())

但控制台中没有记录任何内容。

也许我以JavaScript的方式做这件事,因为那是我的背景。

正确的方法是什么?

修改

这就是SendRequest内的内容:

def SendRequest(self, endpoint, post = None, login = False):
        if (not self.isLoggedIn and not login):
            raise Exception("Not logged in!\n")
            return;

        self.s.headers.update ({'Connection' : 'close',
                                'Accept' : '*/*',
                                'Content-type' : 'application/x-www-form-urlencoded; charset=UTF-8',
                                'Cookie2' : '$Version=1',
                                'Accept-Language' : 'en-US',
                                'User-Agent' : self.USER_AGENT})

        if (post != None): # POST
            response = self.s.post(self.API_URL + endpoint, data=post) # , verify=False
        else: # GET
            response = self.s.get(self.API_URL + endpoint) # , verify=False

        if response.status_code == 200:
            self.LastResponse = response
            self.LastJson = json.loads(response.text)
            return True
        else:
            print ("Request return " + str(response.status_code) + " error!")
            # for debugging
            try:
                self.LastResponse = response
                self.LastJson = json.loads(response.text)
            except:
                pass
            return False

    def getTotalFollowers(self,usernameId):
        followers = []
        next_max_id = ''
        while 1:
            self.getUserFollowers(usernameId,next_max_id)
            temp = self.LastJson

            for item in temp["users"]:
                followers.append(item)

            if temp["big_list"] == False:
                return followers            
            next_max_id = temp["next_max_id"]         

    def getTotalFollowings(self,usernameId):
        followers = []
        next_max_id = ''
        while 1:
            self.getUserFollowings(usernameId,next_max_id)
            temp = self.LastJson

            for item in temp["users"]:
                followers.append(item)

            if temp["big_list"] == False:
                return followers            
            next_max_id = temp["next_max_id"] 

    def getTotalUserFeed(self, usernameId, minTimestamp = None):
        user_feed = []
        next_max_id = ''
        while 1:
            self.getUserFeed(usernameId, next_max_id, minTimestamp)
            temp = self.LastJson
            for item in temp["items"]:
                user_feed.append(item)
            if temp["more_available"] == False:
                return user_feed
            next_max_id = temp["next_max_id"]

1 个答案:

答案 0 :(得分:1)

如果你要做的只是打印你得到的回复,你可以在sqlContext.sql("select encounter.Member_ID AS patientID, encounter.Encounter_DateTime AS date, diag.code from encounter join diag on encounter.Encounter_ID = diag.Encounter_ID").show(1) 中做到这一点,但我怀疑你真正的问题是你在{{1}时自行序列化你的帖子数据为你这样做。无论如何,因为你的问题是打印:

SendRequest