怎么定义类,类名要大写,类要实例化才才能用,调用类里的方法
class My:
def func(self):
self.name='hahh'
pass
def func2(self):
self.func()
m=My()
m.func()
私有只能再类里面,继承怎么继承
class Person:
def __init__(self,id,name):
self.id=id#实例变量
self.name=name
#只要加了self.在这个类里面都能用
def cook(self):
print('%s鱼香肉丝'%self.name)
def house(self):
print('%s做家务'%self.name)
xm=Person(1,'小黑')
#实例变量 经过实例化之后才能实例的
xm.house()
xm.cook()
xb=Person(2,'小白')
xb.cook()
xb.house()
import requests
class Myrequest:
def __init__(self,url,method='get',data=None,headers=None,is_json=False):
method=method.lower()
self.url=url
self.data=data
self.headers=headers
self.is_json=is_json
if hasattr(self,method):#传一个字符串找到对应方法
getattr(self,method)()
def get(self):
try:
req=requests.get(self.url,self.data,headers=self.headers).json()
except Exception as e:
self.respone={"error":"接口请求出错%s"%e}
else:
self.respone=req.json()
def post(self):
try:
if self.is_json:
req=requests.post(self.url,json=self.data,headers=self.headers).json()
else:
req = requests.post(self.url, self.data, headers=self.headers).json()
except Exception as e:
self.respone={"error":"接口请求不通"}
else:
self.respone=req.json()
if __name__== '__main__':
# Login=Myrequest('')
m=Myrequest('http://10.216.1.154:8081/misc/fieldsinfo','get',headers='PN=EhuHxnA1c2J9Iupx4FeYVKwOT1JEYOtk57vgftmqBC2Qok5XGY1JWOVgTTueMbpaBoOCArl64f1N5HFxQYgsyoKYWDeGPq5tCzjctqhBLWm%2Fu%2BsjNqVkmgEPzm8%2FFr7Db3y6e83iwvDjWm33ZM82rutrEEhDvhl%2BTaoGLQSrDtiMA9YIRnGp4xaX%2Flb742czfGAr%2FgmHSOiSovwqmOD7XvLJ50V13FyysavErnjEThDk%2BmXoZABKNowtTFFjGYJQISNurPOIqML1ov2QjZRMrXvCAOc5iaNzmuJ%2BSZYDdshXdjawttIKDxh0dd%2FrtoOiaz%2BI2QTdcDKms2k9xOExC8QSEPMtv1GGzGMLy%2Fza1P9CKwwlcz0ASvmzPkXeolZv')
print(m.respone)
朝花夕拾:
headers=
朝花夕拾:
是一个字典
董:
我直接传的cookie的值这样不行莫
朝花夕拾:
那你要写
朝花夕拾:
headers = {'cookie':'xxxxxxxxx'}