python-10-socket发送http协议来请求

1.模拟客户端(http协议拼接请求报文并发送)

    import socket
    client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client_socket.connect(('192.168.43.239',8080))
   
    file_path=input('请输入要请求的文件地址')
    
    request_line='GET ./%s HTTP/1.1\r\n'%file_path            #请求行
    request_header='Host=www.111.com\r\n'                     #请求头
    request_blank='\r\n'                                      #请求空行
    request_data=request_line+request_header+request_blank    #拼接请求报文
    
    client_socket.send(request_data.encode())
    content=client_socket.recv(40960).decode()
  
    if not content:                                           #没有响应主体,说明接收文件失败
        print('接收文件失败')
    else:
        print('正在接收服务端返回信息')
        with open('5.html','w')as file:
            loc=content.find('\r\n\r\n')
            body=content[loc+4:]
            file.write(body)
            print('已经保存信息')
    
    client_socket.close()

2.服务端

  import socket
    import time
    class Server():
        def __init__(self):                                                              #初始化                    
            tcp_server_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)           #创建套接字
            tcp_server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,True)     #设置地址重用
            tcp_server_socket.bind(('192.168.43.239',8080))                              #绑定端口
            tcp_server_socket.listen(128)                                                #设置监听
            self.tcp_server_socket=tcp_server_socket                                     #其他函数需要用到的实例属性
       
        def addbaowen(self,reponse_body):                                                #拼接HTTP响应报文      
            reponse_line='HTTP/1.1 200 OK \r\n'
            reponse_headle='SERVER=BWC/1.0\r\n'
            reponse_blank='\r\n'
            reponse_data=(reponse_line+reponse_headle+reponse_blank+reponse_body).encode()
            return  reponse_data
        
        def start(self):
                new_client_soket,ip_port=self.tcp_server_socket.accept()
                request_data=new_client_soket.recv(1024).decode()
                if request_data:                                                         #判断客户端是否断开连接
                    loc=request_data.find('\r\n')
                    request_list=request_data[:loc].split(' ')
                    file_path=request_list[1]
                    if file_path=='./':                                                  #判断客户端是否输入了目录资源地址,没输入则默认资源地址
                        file_path='static/index.html'
                    print('正在请求客户端要求的%s资源地址'%file_path)
                    time.sleep(1)                                                        #  纯属视觉效果,等待1s
                    try:                                                                 # 捕获异常,当传来的文件资源地址不存在时,捕获错误
                        reponse_data=self.handle(file_path)
                        new_client_soket.send(reponse_data)
                    except Exception as e:
                        print('对方发来的文件地址有误')
                        return
                    print('文件已经成功发送完毕')
                else:                                                                    #客户端断开连接,关闭套接字
                    new_client_soket.close()
                    return
        def handle(self,file_path):
            with open(file_path,'r')as file:
                reponse_body=file.read()
                reponse_data=self.addbaowen(reponse_body)
                return reponse_data
    if __name__ == '__main__':
        ws=Server()
        ws.start()


                                                          

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               


你可能感兴趣的:(HTTP客户端服务端)