一个专注于计算机编程和硬件学习的博客主
23/3
2019

HTTP原理

安装nmap

sudo apt-get install nmap

安装


通过TCP测试ncat

  • 同时打开两个命令窗口

    • 左边的充当服务器,打开9999端口

      • ncat -l 9999
    • 右边访问本地服务器9999端口

      • ncat localhost 9999
    • 在左边窗口输入句子,右边可显示

模拟访问


架设第一台服务器

  • 将自己所写的网页文件放入一个文件夹中
  • 在控制台进入次文件夹

    • python3 -m http.server 9999
      • 创建一个本地服务器

网页文件

  • 如果是在自己的本地Linux电脑,可在浏览器直接输入:

    • http://localhost:9999
      • 访问你所上传的网页
  • 因我是使用的树莓派进行练习,通过ssh连接,所以需要在浏览器输入,树莓派的ip地址访问:

    • http://192.168.43.120:9999

查看网页

请求查询

  • 网页有访问时,服务器有相应操作记录

localhost与端口

  • 利用host查看本地服务:

    • host localhost

查看本地服务

  • 端口

    • http默认端口:80
    • https默认端口:443

通过ncat手动get网页

  • 在当前控制台保持服务器状态,打开新控制台

    • ncat 127.0.0.1 8000

手动请求

  • 在右侧输入:

    • GET /HTTP/1.1
    • 再输入:

      • Host: localhost
      • 敲两次回车确认

手动GET


状态码

1xx: 消息

  • 100: Continue
  • 101: Switching Protocols
  • 102: Processing

2xx: 成功

  • 200: OK
  • 201: Created
  • 202: Accepted
  • 203: Non-Authoritative Information
  • 204: No Content
  • 205: Reset Content
  • 206: Partial Content
  • 207: Multi-Status
  • 208: Already Reported
  • 226: IM Used

3xx重定向

  • 300: Multiple Choices
  • 301: Moved Permanently
  • 302: Found
  • 303: See Other
  • 304: Not Modified
  • 305: Use Proxy
  • 306: Switch Proxy
  • 307: Temporary Redirect
  • 308: Permanent Redirect

4xx客户端错误

  • 400: Bad Request
  • 401: Unauthorized
  • 402: Payment Required
  • 403: Forbidden
  • 404: Not Found
  • 405: Method Not Allowed
  • 406: Not Acceptable
  • 407: Proxy Authentication Required
  • 408: Request Timeout
  • 409: Conflict
  • 410: Gone
  • 411: Length Required
  • 412: Precondition Failed
  • 413: Request Entity Too Large
  • 414: Request-URI Too Long
  • 415: Unsupported Media Type
  • 416: Requested Range Not Satisfiable
  • 417: Expectation Failed
  • 418: I'm a teapot
  • 420: Enhance Your Caim
  • 421: Misdirected Request
  • 422: Unprocessable Entity
  • 423: Locked
  • 424: Failed Dependency
  • 425: Unordered Collection
  • 426: Upgrade Required
  • 428: Precondition Required
  • 429: Too Many Requests
  • 431: Request Header Fields Too Large
  • 444: No Response
  • 450: Blocked by Windows Parental Controls
  • 451: Unavailable For Legal Reasons
  • 494: Request Header Too Large

5xx服务器错误

  • 500: Internal Server Error
  • 501: Not Implemented
  • 502: Bad Gateway
  • 503: Service Unavailable
  • 504: Gateway Timeout
  • 505: HTTP Version Not Supported
  • 506: Variant Also Negotiates
  • 507: Insufficient Storage
  • 508: Loop Detected
  • 510: Not Extended
  • 511: Network Authentication Required

非官方状态码

  • 420: Enhance Your Calm
  • 498: Invalid Token
  • 499: Token Required
  • 520: Unknown Error
  • 521: Web Server Is Down

将用户重定向

  • 创建服务器

    • ncat -l 9999
  • 在浏览器访问此端口

    • http://localhost:9999

重定向

  • 在命令窗口输入:

    • HTTP/1.1 307 Temporary Redirect
      Location: http://www.chenxiao.xyz
    • 敲两次回车,进行重定向

通过class代替手动输入

from http.server import HTTPServer, BaseHTTPRequestHandler

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content_type','text/plain;charset=utf-8')
        self.end_headers()
        self.wfile.write("Hello every, I am chenxiao,I am a AI server~\n".encode())

if __name__ == '__main__':
    server_address=('',9999)
    httpd=HTTPServer(server_address,Handler)
    httpd.serve_forever()

跑一下手写的服务

  • 转到.py文件所在的目录
python3 文件.py

启动程序

  • 浏览器访问

浏览器访问


搜索查询网址

网址分析

  • 利用python将搜索的网址进行拆分

    • from urllib.parse import urlparse, parse_qs
      #导入库
      #复制网址
      url = "https://cn.bing.com/search?q=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9F%E6%83%B3%E5%90%83%E7%B3%96&qs=n&form=QBRE&sp=-1&pq=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9Fx%27c%27t&sc=0-10&sk=&cvid=F8240A97F07E4D1A8D01FD24E57E758D"
      parts = urlparse(url)
      print(parts)
      print(parts.query)
      query = parse_qs(parts.query)
      print(query)
    • ParseResult(scheme='https', netloc='cn.bing.com', path='/search', params='', query='q=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9F%E6%83%B3%E5%90%83%E7%B3%96&qs=n&form=QBRE&sp=-1&pq=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9Fx%27c%27t&sc=0-10&sk=&cvid=F8240A97F07E4D1A8D01FD24E57E758D', fragment='')
      q=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9F%E6%83%B3%E5%90%83%E7%B3%96&qs=n&form=QBRE&sp=-1&pq=%2B%E6%B2%89%E6%BD%87%E5%85%88%E7%94%9Fx%27c%27t&sc=0-10&sk=&cvid=F8240A97F07E4D1A8D01FD24E57E758D
      {'q': ['+沉潇先生想吃糖'], 'qs': ['n'], 'form': ['QBRE'], 'sp': ['-1'], 'pq': ["+沉潇先生x'c't"], 'sc': ['0-10'], 'cvid': ['F8240A97F07E4D1A8D01FD24E57E758D']}

已有 4 条评论

    1. 是我哪写错了吗?

      1. 错是没错,就发个表情刷下存在感

添加新评论