简单web服务器

背景是我带了两个小朋友,最近有个自学任务,要搭建一个简单的web服务器。猜课程的目的应该是考察协议的设计。

  1. 利用现成的框架
  2. 利用Java中现成函数、利用现成的C语言库

我晚上抽一个小时简单整理下,权做抛砖引玉吧。

利用现成的框架

sample.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

__author__ = 'Yeshen'

from bottle import run, post, request, response, get, route

@get('/')
def login():
    return '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1" /><title>YESHEN</title><style type="text/css">nav {color: #999;margin: 20% auto;text-align: center;}</style></head><body><nav><h1>2020</h1><p>趣果有间</p><p></p></nav></body></html>'

if __name__ == "__main__":
    run(host='', port=12345, debug=False)
pip intstall bottle
python sample.py
# http://127.0.0.1:12345

PS:
bottle算是很简单的框架了,平时开发过程中有什么接口服务端给不了的,自己部署一个也不会太难。
文档参考:https://bottlepy.org/docs/dev/tutorial.html

利用Java中现成函数

sample.java

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.util.Date;
import java.util.StringTokenizer;

/**
* Written by Martin Ombura Jr. <@martinomburajr>
* Modify by Yeshen at March 16.2020
*/
public class sample {
    public static void main(String[] args) {
        connectToServer();
    }

    public static void connectToServer() {
        BufferedReader in = null; 
        PrintWriter out = null; 
        Socket connectionSocket = null;
        try {
            ServerSocket serverSocket = new ServerSocket(12345);
            connectionSocket = serverSocket.accept();

            in = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            out = new PrintWriter(connectionSocket.getOutputStream());
            InputStream inputToServer = connectionSocket.getInputStream();
            OutputStream outputFromServer = connectionSocket.getOutputStream();
            String input = in.readLine();
            StringTokenizer parse = new StringTokenizer(input);
            String method = parse.nextToken().toUpperCase(); 
            String content = "<!DOCTYPE html><html><head><meta charset='UTF-8'><meta name='viewport' content='width=device-width, initial-scale=1' /><title>YESHEN</title><style type='text/css'>nav {color: #999;margin: 20% auto;text-align: center;}</style></head><body><nav><h1>2020</h1><p>趣果有间</p><p></p></nav></body></html>";
            
            if (method.equals("GET")) {
                out.println("HTTP/1.1 200 OK");
                out.println("Server: Java HTTP Server from SSaurel : 1.0");
                out.println("Date: " + new Date());
                out.println("Content-type: text/html");
                out.println("Content-length: " + content.length());
                out.println(); 
                out.flush();
                out.println(content);
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(in != null)in.close();
            } catch (Exception e) {
                System.err.println("Error closing stream : " + e.getMessage());
            }
            try {
                if(out != null)out.close();
            } catch (Exception e) {
                System.err.println("Error closing stream : " + e.getMessage());
            } 
            try {
                if(connectionSocket != null)connectionSocket.close();
            } catch (Exception e) {
                System.err.println("Error closing stream : " + e.getMessage());
            } 
        }
    }
}
javac sample.java
java sample
# http://127.0.0.1:12345

想了一下,我不能全部都做完。

  1. 自学文档中还剩下一些东西,研究下去,弄出来?
  2. 用其他语言如何实现这个功能(kotlin\c++)?
  3. 这个功能在Android中如何实现?
  4. 如何抓包?

参考资料

  1. https://bottlepy.org/docs/dev/tutorial.html
  2. https://medium.com/martinomburajr/java-create-your-own-hello-world-server-2ca33b6957e
相关推荐
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页