alexchenco alexchenco - 1 month ago 5
Python Question

How do you print a function that returns a request in Python?

I have a function that gets the profile data of an user:

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))


I want to print the returned request in the terminal, so I did this:

test.py

from API import API

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


But nothing is logged in the console.

Maybe I'm doing it the JavaScript way, since that's my background.

What's the correct way to do it?

EDIT:

This is what's inside
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"]

2ps 2ps
Answer

If all you want to do is print the response that you get back, you can do that in SendRequest, but I suspect tha tyour real problem is that you are self-serializing your post data when requests does that for you. In any case, since your question is about printing:

    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
Comments