python实现httpclient(http.client)访问地址

import http.client as hc

# java开发者思路的错误示范
def sendRequest():
    con = hc.HTTPConnection(host="cn.bing.com")
    con.request(method='GET', url='https://cn.bing.com/robots.txt')
    response = con.getresponse()
    restudy = bytearray(10240)
    i = response.readinto(restudy)
    res = bytes(restudy).decode('utf-8')
    # restudy.zfill(1024)
    while i != 0:
        i = response.readinto(restudy)
        res = res + bytes(restudy).decode('utf-8')
        # restudy.zfill(1024)
    print(res)
    response.flush()
    response.close()
    con.close()


def chatAns():
    # 设置要访问的链接和请求方法
    conn = hc.HTTPSConnection("cn.bing.com")
    conn.request("GET", "/robots.txt")

    # 获取响应
    res = conn.getresponse()

    # 输出响应状态码
    print(res.status)

    # 输出响应头部信息
    print(res.getheaders())

    # 输出响应正文
    print(res.read().decode("utf-8"))


if __name__ == '__main__':
    print("n")
    chatAns()
您的感觉是什么
更新 2023年3月23日