Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Client/Client
Binary file not shown.
20 changes: 20 additions & 0 deletions Client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"fmt"
"log"
"net/http"
"net/http/httputil"
)

func main() {
// 指定したURLにGETを発行する
res, err := http.Get("http://localhost:8080/hi")
if err != nil {
log.Fatal(err)
}

// GETのレスポンスの取得
dumpRes, _ := httputil.DumpResponse(res, true)
fmt.Printf("%s", dumpRes)
}
17 changes: 17 additions & 0 deletions Client/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# クライアントを作成

import socket

bufsize = 4096
url = b"http://3.113.12.158/hi"

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# サーバを指定
s.connect(("3.113.12.158", 80))
# サーバにメッセージを送る
s.sendall(url)
# サーバからの文字列を取得する
response = s.recv(bufsize)
print(response.decode("utf-8"))
# ip = s.recv(bufsize)
# print(ip.decode("utf-8"))
16 changes: 16 additions & 0 deletions Curl.md
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
# Please show HTTP Request and Response using curl command
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /hi HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.63.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Hello: BasicHTTP!
< Date: Sat, 06 Jun 2020 16:57:48 GMT
< Content-Length: 36
< Content-Type: text/plain; charset=utf-8
<
* Connection #0 to host localhost left intact
192.168.10.5fe80::c1e:fd62:eb52:bc58
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
15 changes: 15 additions & 0 deletions Server/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
# Please write code of simple HTTP(Web) server

write this directory

## Specification

- write by using favorit language

- 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

```
Binary file added Server/Server
Binary file not shown.
60 changes: 60 additions & 0 deletions Server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"net"
"net/http"
"os"
"strings"
)

// API "/hi"のときに呼ばれる関数
func handleHi(w http.ResponseWriter, req *http.Request) {
// header

// 送られてきたHTTPリクエストメソッドを取得
method := req.Method
fmt.Println("Get GETRequest!")
fmt.Println("[method]" + method)

// HTTPリクエストヘッダの取得
for key, value := range req.Header {
fmt.Print("[header]" + key)
fmt.Println(": " + strings.Join(value, ","))
}
fmt.Print("\n")

// GET
if method == "GET" {

// ヘッダの追加
w.Header().Set("Hello", "BasicHTTP!")

// IPアドレスの取得
hostname, err := os.Hostname()
if err != nil {
fmt.Printf("hostnameが取れません: %v\n", err)
return
}

addrs, err := net.LookupIP(hostname)
if err != nil {
fmt.Printf("IPが取れません: %v\n", err)
return
}

for _, a := range addrs {
// Fprintで書き込み先を明示的に指定
fmt.Fprint(w, a)
}

}
}

func main() {
// API(URL)によって呼び出す関数を決める。ルーティング的な?
http.HandleFunc("/hi", handleHi)
fmt.Println("Server start!")
// ポートの指定とサーバの起動
http.ListenAndServe(":8080", nil)
}
47 changes: 47 additions & 0 deletions Server/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# socket サーバを作成
import socket
import datetime
bufsize = 4096

host = socket.gethostname()
ip = socket.gethostbyname(host)
# today = datetime.datetime.now(datetime.timezone.utc)
# print(today)

# AF = IPv4 という意味
# TCP/IP の場合は、SOCK_STREAM を使う
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# IPアドレスとポートを指定
s.bind((socket.gethostname(),80))
# 1 接続
s.listen(1)
# connection するまで待つ
while True:
print("server 起動")
# 誰かがアクセスしてきたら、コネクションとアドレスを入れる
conn, addr = s.accept()
with conn:
url = conn.recv(bufsize)
print(url.decode())
if "/hi" in url.decode():
conn.send(bytes("HTTP/1.1 200 OK\n"
+"Hello: BasicHTTP!\n"
+"Date: Tue, 26 May 2020 06:53:14 GMT\n"
+"Content-Length: 1\n"
+"Content-Type: text/html\n"+"\n{0}".format(ip), "utf-8"))
else:
break

# conn.send(bytes(ip, "utf-8"))


# while True:
# # データを受け取る
# url = conn.recv(1024).decode("utf-8")
# print(url.split("/"))
# if not url:
# break
# # print('data : {}, addr: {}'.format(data, addr))
# # クライアントにデータを返す(b -> byte でないといけない)
# # conn.sendall(b'Received: ' + data)
# conn.send(bytes("server ip: {0}".format(ip), "utf-8"))