使用python直接连接http服务器获取数据


代码如下

import socket

SPLIT_LINE = '\r\n'


def connect(host, port, req_url):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))
    sd = 'GET ' + req_url + ' HTTP/1.1' + SPLIT_LINE
    sd = sd + get_header(host, port)
    sd = sd + SPLIT_LINE
    s.send(sd.encode())
    data = s.recv(1024)
    print(data)
    while True:
        data2 = s.recv(1024)
        if not data2:
            break
        print(data2)
    s.close()


def get_header(host, port):
    s = 'Accept:text/html,application/xhtml+xml,application/xml'+SPLIT_LINE
    s = s + 'Accept-encoding:gzip, deflate, br' + SPLIT_LINE
    s = s + 'Accept-Language:zh-CN,zh;q=0.9' + SPLIT_LINE
    s = s + 'Cache-Control:max-age=0' + SPLIT_LINE
    s = s + 'Connection:keep-alive' + SPLIT_LINE
    s = s + 'Host:' + host + ':' + str(port) + SPLIT_LINE
    s = s + 'User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36' + SPLIT_LINE
    return s


if __name__ == '__main__':
    req_url = '/hello/world'
    connect('localhost', 8080, req_url)

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM