首页 > 编程语言 > 详细

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

时间:2021-06-08 09:43:41      阅读:18      评论:0      收藏:0      [点我收藏+]

代码如下

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)

  

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

原文:https://www.cnblogs.com/mark200106/p/14860938.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!