遍历对象的属性

函数vars

for f,v in vars(resp).items():
    print(f"{f}:{v}")

dict属性

for attr, val in resp.__dict__.items():
    print(f"{attr}:{val}")

dir函数

print(dir(resp))

网络访问

requests库

get请求

import requests
res = requests.get('http://smartnexus.cn/')
print(res.status_code)

post请求

data={'name':'zs','passwd':'123'}
res = requests.post('http://smartnexus.cn/',json=data)
print(res.status_code)

http.client模块

import http.client
conn = http.client.HTTPConnection("smartnexus.cn")
conn.request('GET',"/console")
resp = conn.getresponse()
print(resp.read())

高级

生成器

g = (x for x in range(20) if x % 2 == 0)
for i in g:
    print(i)
def test():
    a,b = 0,1
    while True:
        yield b
        a,b = b,(a + b)
    return "done"

g = test()
s = 0
for i in g:
    if s > 20:
        break
    print(i)
    s = s + 1
for i in range(10):
    print(next(g))
for i in range(10):
    print(g.__next__

高阶函数

def test(x):
    if x == 1:
        return x
    else:
        return x * test(x -1)
def test1(fun, lst):
    new_list = []
    for i in lst:
        new_list.append(fun(i))
    return new_list

print(test1(test, [i for i in range(1, 10)]))

print(list(map(lambda x:x%2==0,(i for i in range(10)))))

from functools import reduce
print(reduce(lambda x,y:x+y, [i for i in range(10)], 10))

闭包

def test(x):
    def test1(y):
        return x * y
    return test1

f = test(10)
print(f(20))

课程笔记

  1. 在进程中父进程的全局变量不可以被子进程共享,在线程中却可以