2016-10-09 122 views
4

我有獲取一個用戶的配置文件數據的功能:如何打印一個在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"] 
+0

檢查並看看'self.SendRequest'返回什麼? –

回答

1

如果你想要做的是打印的答覆中說你回來,你能做到這一點在SendRequest,但我懷疑tha tyour真正的問題在於,當requests爲你做這件事時,你正在自我序列化你的發佈數據。在任何情況下,因爲你的問題是關於打印:

if response.status_code == 200: 
     print('Yay, my response was: %s' % response.content) 
     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