在python版本2.7.1中的rest客户端示例

时间:2017-12-22 07:08:26

标签: python

以下是我编写的代码,但在执行此代码时,没有任何事情发生。我的意思是没有错误,也没有显示出正确的结果。我只能看到一个空白python shell,其他python程序正常运行。我正在使用python version 2.7.1

API_call.py:

import requests
import json

class API_call:
    def GET_call(self):
        print 'Inside GET_Call method'
        response= requests.get('https://stackoverflow.com')
        if response.status_code==200:
            print 'URL ACTIVE'
            print (response.json)
        elif response.status_code!=200:
            print 'URL INACTIVE'
            print (response.content)
            print (response.json)


api_call= API_call()
api_call.GET_call

输出:

====================== RESTART: C:/Python27/API_call.py ======================

1 个答案:

答案 0 :(得分:0)

您缺少函数调用()。此外,if a == True: ... elif a != True: ...if a == True: ... else: ...完全相同,性能更差。条件被检查两次而不是一次,它需要更多的代码。

import requests
import json

class API_call:
    def GET_call(self):
        print 'Inside GET_Call method'
        response = requests.get('https://stackoverflow.com')
        if response.status_code == 200:
            print 'URL ACTIVE'
            print (response.json)
        else:
            print 'URL INACTIVE'
            print (response.content)
            print (response.json)


api_call = API_call()
api_call.GET_call()