diff --git a/Client/client.py b/Client/client.py new file mode 100644 index 0000000..ea3f783 --- /dev/null +++ b/Client/client.py @@ -0,0 +1,14 @@ +import socket + +url = b"http://localhost:8080/hi/" + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + + s.connect(("localhost:8080", 80)) + + s.send(url) + resp = s.recv(4096) + ip = s.recv(4096) + + print(resp.decode("utf-8")) + print(ip) diff --git a/README.md b/README.md index 99b6488..1f3606e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ # Exp3BasicHttp For Labo of Basic HTTP for Exp3 of Univ +## Mission + +1. show Server Directory README.md +2. show Client Directory README.md +2. show Curl.md + ## Setup 1. access 'https://github.com/Hardw01f/Exp3BasicHttp' diff --git a/Server/README.md b/Server/README.md index 27b8f2a..572e856 100644 --- a/Server/README.md +++ b/Server/README.md @@ -1,5 +1,7 @@ # Please write code of simple HTTP(Web) server +write this directory + ## Specification - write by using favorit language @@ -7,3 +9,16 @@ - listen the API '/hi' - return your IP address in Response Body - return 'Hello: BasicHTTP!' Header in Response Header + +## Response Exsample + +``` +HTTP/1.1 200 OK +Hello: BasicHTTP! +Date: Tue, 26 May 2020 06:53:14 GMT +Content-Length: 13 +Content-Type: text/plain; charset=utf-8 + +10.10.11.194 + +``` diff --git a/Server/server.py b/Server/server.py new file mode 100644 index 0000000..8e67072 --- /dev/null +++ b/Server/server.py @@ -0,0 +1,30 @@ +import socket +import datetime + +dt = datetime.datetime.now() + +host = socket.gethostname() +ip = socket.gethostbyname(host) +print(ip) +url = b"http://localhost:8080/hi/" + +with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind((host, 80)) + s.listen(1) + + while True: + conn, addr = s.accept() + with conn: + while True: + data = conn.recv(1024) + print(data) + + if data == url: + print('data : {}, addr: {}'.format(data, addr)) + #conn.sendall(b'Received: ' + data) + conn.send(bytes("HTTP/1.1 200 OK\n" + "Hello: BasicHTTP!\n" + + "Date: Tue, 26 May 2020 06:53:14 GMT\n" + + "Content-Length: 13\n" + "Content-Type: text/plain;", "utf-8\n" + "\n")) + + conn.send(bytes(ip,"utf-8")) + break \ No newline at end of file diff --git a/response.txt b/response.txt new file mode 100644 index 0000000..f8b08a4 --- /dev/null +++ b/response.txt @@ -0,0 +1,29 @@ +#awsのec2にserver.pyを置いて, localとremoteで試したやり方 +e185729% python client.py [~] +HTTP/1.1 200 OK +Hello: BasicHTTP! +Date: Tue, 26 May 2020 06:53:14 GMT +Content-Length: 13 +Content-Type: text/plain; +b'10.10.30.181' +e185729% + +#curlコマンドを叩いて行ったやり方 +e185729% curl -v http://54.92.28.20/hi/ [~] +* Trying 54.92.28.20... +* TCP_NODELAY set +* Connected to 54.92.28.20 (54.92.28.20) port 80 (#0) +> GET /hi/ HTTP/1.1 +> Host: 54.92.28.20 +> User-Agent: curl/7.54.0 +> Accept: */* +> +< HTTP/1.1 200 OK +< Hello: BasicHTTP! +< Date: Tue, 26 May 2020 06:53:14 GMT +< Content-Length: 13 +* transfer closed with 13 bytes remaining to read +* stopped the pause stream! +* Closing connection 0 +curl: (18) transfer closed with 13 bytes remaining to read +