1. 程式人生 > >socket 模擬 HTTP請求

socket 模擬 HTTP請求

一、socket介紹

二、python socket程式設計

二、socket 模擬 HTTP請求

import socket
from urllib.parse import urlparse


class ParserUrl(object):
    '''
    對url進行解析,並返回域名和路徑
    '''

    def __init__(self, url):
        self.url = url

    def get_host_path(self):
        parser_url = urlparse(self.url)
        host, path = parser_url.netloc, parser_url.path
        if path:
            return host, path
        else:
            return host, '/'


class SocketHttp(object):
    '''
    需要一個ParserUrl物件,獲取host和path
    建立連線返回http response字串
    http資料傳遞的時是以位元組為單位的,所以需要編碼
    '''

    def __init__(self, parser_url):
        self.host, self.path = parser_url.get_host_path()


    def set_socket(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client.connect((self.host, 80))
        self.client.send("GET {} HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(self.path, self.host).encode("utf8"))

    @property
    def data(self):
        self.set_socket()
        data = b""
        while True:
            d = self.client.recv(1024)
            if d:
                data += d
            else:
                break
        self.close_socket()
        return data.decode("utf8")

    @property
    def html_content(self):
        html_data = self.data
        return html_data.split("\r\n\r\n")[1]

    def close_socket(self):
        self.client.close()


if __name__ == "__main__":
    url = ParserUrl('https://www.baidu.com/')
    socket_http = SocketHttp(url)
    print(socket_http.html_content)

返回結果

返回正常的字串