SlideShare a Scribd company logo
1 of 25
Download to read offline
Gunosy.go #6
net/http + net/url
自己紹介
• 印南 聡志 (いんなみ さとし)
• 6月にGunosyに入社
• https://github.com/satoshi03
• Java/Ruby … 最近 Python/Go 始めました
Package net/http + net/url
• net
• net/http
– cgi
– cookiejar
– httptest
– httputil
– pprof
• net/url
物理層
データリンク層
ネットワーク層 (IP)
トランスポート層(TCP/UDP)
アプリケーション層 (HTTP)
Package net
• Package net provides a portable interface for
network I/O, including TCP/IP, UDP, domain name
resolution, and Unix domain sockets.
• 比較的低レイヤーの通信を実現
物理層
データリンク層
ネットワーク層 (IP)
トランスポート層(TCP/UDP)
アプリケーション層 (HTTP)
Package net
Server Client
Listen()
Accept()
Dial()
Write()
Read()
Write()
Read()
net server
• func Listen(network, laddr string) (Listener, error)
– クライアントからのコネクションするための接続口を作成
– laddr : ローカルアドレス
– network : “tcp”, “tcp4”, “tcp6”, “unix”, “unixpacket”
• 指定可能 : stream orientedなもの
• それ以外 : エラー
– Example :
• Listen(“tcp” “:8080”)
net client
• func Dial(network, address string) (Conn, error)
– 指定したアドレスにnetworkで指定した方法(≒Protocol)で
接続
– Network に指定する値
• tcp, tcp4, tcp6m udp, udp4, udp6, ip, ip4, ip6, unix, unixgram,
unixpacket
– Examples
• Dial(“tcp”, “12.34.56.78:80”)
• Dial(“tcp”, “google.com:http”)
• Dial(“tcp”, “[2001:db8::1]:http”)
• Dial(“tcp”, “[fe80::1%lo0]:80”)
net server
• Func Accept(network, address string) (Conn, error)
– クライアントからの接続を受諾し、Connオブジェクトを返信
– network : “tcp”, “tcp4”, “tcp6”, “unix”, “unixpacket”
• 指定可能 : stream orientedなもの
• それ以外 : エラー
Demo
Package http
• Package http provides HTTP client and server
implementations.
物理層
データリンク層
ネットワーク層 (IP)
トランスポート層(TCP/UDP)
アプリケーション層 (HTTP)
Package http
http.Server
http.Handler
http.Client
ListenAndServe
:80
GET/POST/HEAD
http server
• type Server
type Server struct {
Addr string
Handler Handler
ReadTimeout time.Duration
WriteTimeout time.Duration
MaxHeaderBytes int
TLSConfig *tls.Confis
}
http server
func ListenAndServe(addr string, handler Handler) error
– 指定したアドレス、ハンドラーを使用してサーバー
を起動
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
http server demo
type AppHandler struct {
}
func(index *AppHandler) ServeHTTP(w
http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, »hello world »)
}
func main() {
index := new(AppHandler)
http.ListenAndServe(":8080", index)
}
Package http
http.Server
http.Handler http.Handler http.Handler
http.Client
ListenAndServe
:80
GET/POST/HEAD
http.ServeMux
http server
• func Handle(pattern string, handler Handler)
– 指定したパターンでハンドラーを追加
• func HandleFunc(pattern string, handler
func(ResponseWriter, *Request))
– 指定したパターンでファンクションハンドラーを追加
http server demo
type IndexHandler struct {
}
func(index *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is index page.”)
}
type DetailHandler struct {
}
func(index *DetailHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is detail page.")↲
}
func main() {
index := new(IndexHandler)↲
detail := new(DetailHandler)↲
http.Handle("/", index)
http.Handle("/detail", detail)
http.ListenAndServe(":8080", nil)
}
http client (1/3)
• func (c *Client) Get(url string) (resp *Response, err
error)
– 指定したURLに対してGETリクエストを送信してレスポンス
を取得
– url : URL文字列
http client(2/3)
• func (c *Client) Post(url string, bodyType string, body
io.Reader) (resp *Response, err error)
– 指定したURLに対してPOSTリクエストを送信
– url : url文字列
– bodyType : post時に送信するバイト列の種類
– body : 送信するバイト列
– Example:
• resp, err := http.Post(“http://example.com/upload",
"image/jpeg", &buf)
http client(3/3)
• func (c *Client) PostForm(url string, data url.Values)
(resp *Response, err error)
– Key Value形式でPOSTリクエストを送信
– url : url文字列
– data : key value形式のデータ
• Example
– resp, err := http.PostForm("http://example.com/form",
url.Values{"key": {"Value"}, "id": {"123"}})
Package url
• URLを管理するパッケージ
– URLのパース
– クエリーのエスケープ処理
type url
type URL struct {
Scheme string
Opaque string // encoded opaque data
User *Userinfo // username and password information
Host string // host or host:port
Path string
RawQuery string // encoded query values, without '?'
Fragment string // fragment for references, without '#'
}
scheme://[userinfo@]host/path[?query][#fragment]
url
• func Parse(rawurl string) (url *URL, err error)
– URL文字列をパースしてURLオブジェクトを取得
u, err := url.Parse("http://bing.com/search?q=dotnet")
if err != nil {
log.Fatal(err)
}
u.Scheme = "https”
u.Host = "google.com”
q := u.Query()
q.Set("q", "golang")
u.RawQuery = q.Encode()
fmt.Println(u)
> https://google.com/search?q=golang
http://play.golang.org/p/8Id1F0vfvD
url
• QueryEscape(s string) string
– クエリーの文字列のエスケープに変換
• QueryUnescape(s string) (string, error)
– エスケープされたクエリーを文字列に変換
escaped_url := url.QueryEscape(http://bing.com/search?q=test)
fmt.Println("escaped_url : " + escaped_url)
unescaped_url, err := url.QueryUnescape(escaped_url)
if err != nil {
log.Fatal(err)
}
fmt.Println("escaped_url : " + unescaped_url)
escaped_url : http%3A%2F%2Fbing.com%2Fsearch%3Fq%3Dtest
escaped_url : http://bing.com/search?q=test
http://play.golang.org/p/-jZzlqHdXm
Package net
• net
• net/http
– cgi
– cookiejar
– httptest
– httputil
– pprof
• net/url
物理層
データリンク層
ネットワーク層 (IP)
トランスポート層(TCP/UDP)
アプリケーション層 (HTTP)

More Related Content

What's hot

Let's begin WebRTC
Let's begin WebRTCLet's begin WebRTC
Let's begin WebRTCyoshikawa_t
 
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)Panda Yamaki
 
Netty & Apache Camel
Netty & Apache CamelNetty & Apache Camel
Netty & Apache Camelssogabe
 
Scapyで作る・解析するパケット
Scapyで作る・解析するパケットScapyで作る・解析するパケット
Scapyで作る・解析するパケットTakaaki Hoyo
 
Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)ashigirl ZareGoto
 
Hokkaido.cap #osc11do Wiresharkを使いこなそう!
Hokkaido.cap #osc11do Wiresharkを使いこなそう!Hokkaido.cap #osc11do Wiresharkを使いこなそう!
Hokkaido.cap #osc11do Wiresharkを使いこなそう!Panda Yamaki
 
Hokkaido.cap#10 実践パケット解析まとめ
Hokkaido.cap#10 実践パケット解析まとめHokkaido.cap#10 実践パケット解析まとめ
Hokkaido.cap#10 実践パケット解析まとめPanda Yamaki
 
Apache Camel Netty component
Apache Camel Netty componentApache Camel Netty component
Apache Camel Netty componentssogabe
 
CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料SECCON Beginners
 
import dpkt したよ #ssmjp 2014/02/28
import dpkt したよ #ssmjp 2014/02/28import dpkt したよ #ssmjp 2014/02/28
import dpkt したよ #ssmjp 2014/02/28th0x0472
 
URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!treby
 
ブロッキングの技術的課題(公開版)
ブロッキングの技術的課題(公開版)ブロッキングの技術的課題(公開版)
ブロッキングの技術的課題(公開版)UEHARA, Tetsutaro
 
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)Panda Yamaki
 
Beam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタBeam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタKenichi Kambara
 
tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。(^-^) togakushi
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方Satoshi Nagayasu
 
Mongo dbのgridfsについて
Mongo dbのgridfsについてMongo dbのgridfsについて
Mongo dbのgridfsについてMasahiro Saito
 
パケットが教えてくれた ルートサーバが 13個の理由
パケットが教えてくれた ルートサーバが 13個の理由パケットが教えてくれた ルートサーバが 13個の理由
パケットが教えてくれた ルートサーバが 13個の理由@ otsuka752
 
Stream processing and Norikra
Stream processing and NorikraStream processing and Norikra
Stream processing and NorikraSATOSHI TAGOMORI
 
CpawCTF 勉強会 Network
CpawCTF 勉強会 NetworkCpawCTF 勉強会 Network
CpawCTF 勉強会 NetworkTakaaki Hoyo
 

What's hot (20)

Let's begin WebRTC
Let's begin WebRTCLet's begin WebRTC
Let's begin WebRTC
 
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)
Hokkaido.cap#7 ケーススタディ(セキュリティ解析:前編)
 
Netty & Apache Camel
Netty & Apache CamelNetty & Apache Camel
Netty & Apache Camel
 
Scapyで作る・解析するパケット
Scapyで作る・解析するパケットScapyで作る・解析するパケット
Scapyで作る・解析するパケット
 
Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)Scapy presentation Remake(訂正)
Scapy presentation Remake(訂正)
 
Hokkaido.cap #osc11do Wiresharkを使いこなそう!
Hokkaido.cap #osc11do Wiresharkを使いこなそう!Hokkaido.cap #osc11do Wiresharkを使いこなそう!
Hokkaido.cap #osc11do Wiresharkを使いこなそう!
 
Hokkaido.cap#10 実践パケット解析まとめ
Hokkaido.cap#10 実践パケット解析まとめHokkaido.cap#10 実践パケット解析まとめ
Hokkaido.cap#10 実践パケット解析まとめ
 
Apache Camel Netty component
Apache Camel Netty componentApache Camel Netty component
Apache Camel Netty component
 
CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料CTF for ビギナーズ ネットワーク講習資料
CTF for ビギナーズ ネットワーク講習資料
 
import dpkt したよ #ssmjp 2014/02/28
import dpkt したよ #ssmjp 2014/02/28import dpkt したよ #ssmjp 2014/02/28
import dpkt したよ #ssmjp 2014/02/28
 
URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!URIやTEXTをBEAMするアプリを作ったよ!
URIやTEXTをBEAMするアプリを作ったよ!
 
ブロッキングの技術的課題(公開版)
ブロッキングの技術的課題(公開版)ブロッキングの技術的課題(公開版)
ブロッキングの技術的課題(公開版)
 
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)
Hokkaido.cap#5 ケーススタディ(ネットワークの遅延と戦う:後編)
 
Beam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタBeam利用アプリ紹介+関連技術ネタ
Beam利用アプリ紹介+関連技術ネタ
 
tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。tcpdumpとtcpreplayとtcprewriteと他。
tcpdumpとtcpreplayとtcprewriteと他。
 
PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方PostgreSQL - C言語によるユーザ定義関数の作り方
PostgreSQL - C言語によるユーザ定義関数の作り方
 
Mongo dbのgridfsについて
Mongo dbのgridfsについてMongo dbのgridfsについて
Mongo dbのgridfsについて
 
パケットが教えてくれた ルートサーバが 13個の理由
パケットが教えてくれた ルートサーバが 13個の理由パケットが教えてくれた ルートサーバが 13個の理由
パケットが教えてくれた ルートサーバが 13個の理由
 
Stream processing and Norikra
Stream processing and NorikraStream processing and Norikra
Stream processing and Norikra
 
CpawCTF 勉強会 Network
CpawCTF 勉強会 NetworkCpawCTF 勉強会 Network
CpawCTF 勉強会 Network
 

Viewers also liked

PHPerがgolangでもがいてる話@第1回 関西Golang勉強会
PHPerがgolangでもがいてる話@第1回 関西Golang勉強会PHPerがgolangでもがいてる話@第1回 関西Golang勉強会
PHPerがgolangでもがいてる話@第1回 関西Golang勉強会Keisuke Utsumi
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方kwatch
 
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)deris0126
 
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...Hidenori Takeshita
 
alphawing meets heroku
alphawing meets herokualphawing meets heroku
alphawing meets herokuKyosuke Kameda
 
Go言語と過ごした一週間
Go言語と過ごした一週間Go言語と過ごした一週間
Go言語と過ごした一週間Shintaro Kitayama
 
Eureka go 2015_12_12
Eureka go 2015_12_12Eureka go 2015_12_12
Eureka go 2015_12_12matsuo kenji
 
Chrome osとgo言語からgoogleの今後を妄想してみる
Chrome osとgo言語からgoogleの今後を妄想してみるChrome osとgo言語からgoogleの今後を妄想してみる
Chrome osとgo言語からgoogleの今後を妄想してみるMasakazu Muraoka
 
Golang, make and robotics #gocon
Golang, make and robotics #goconGolang, make and robotics #gocon
Golang, make and robotics #goconHideyuki TAKEI
 
勉強会への一歩を踏み出すために
勉強会への一歩を踏み出すために勉強会への一歩を踏み出すために
勉強会への一歩を踏み出すためにAkihiko Horiuchi
 
マイクロサービスにおけるクエリー言語について
マイクロサービスにおけるクエリー言語についてマイクロサービスにおけるクエリー言語について
マイクロサービスにおけるクエリー言語についてsz yudppp
 
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会Satoshi Noda
 
ISUCON2015 PHPで予選を戦ってみた
ISUCON2015 PHPで予選を戦ってみたISUCON2015 PHPで予選を戦ってみた
ISUCON2015 PHPで予選を戦ってみたKen Gotoh
 
ちょっとだけさわってみる Go言語
ちょっとだけさわってみる Go言語ちょっとだけさわってみる Go言語
ちょっとだけさわってみる Go言語Satoshi Noda
 

Viewers also liked (20)

PHPerがgolangでもがいてる話@第1回 関西Golang勉強会
PHPerがgolangでもがいてる話@第1回 関西Golang勉強会PHPerがgolangでもがいてる話@第1回 関西Golang勉強会
PHPerがgolangでもがいてる話@第1回 関西Golang勉強会
 
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
Nippondanji氏に怒られても仕方ない、配列型とJSON型の使い方
 
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)
Introduction to Vim plugins developed by non-Japanese Vimmer (Japanese version)
 
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...
cli.go と cli-init で高速にコマンドラインツールを開発する / The command-line tool developed at hi...
 
alphawing meets heroku
alphawing meets herokualphawing meets heroku
alphawing meets heroku
 
Goを知る
Goを知るGoを知る
Goを知る
 
Go言語と過ごした一週間
Go言語と過ごした一週間Go言語と過ごした一週間
Go言語と過ごした一週間
 
Eureka go 2015_12_12
Eureka go 2015_12_12Eureka go 2015_12_12
Eureka go 2015_12_12
 
Chrome osとgo言語からgoogleの今後を妄想してみる
Chrome osとgo言語からgoogleの今後を妄想してみるChrome osとgo言語からgoogleの今後を妄想してみる
Chrome osとgo言語からgoogleの今後を妄想してみる
 
Github第8章
Github第8章Github第8章
Github第8章
 
らくちん Go言語
らくちん Go言語らくちん Go言語
らくちん Go言語
 
Go+revel
Go+revelGo+revel
Go+revel
 
HighBatch
HighBatchHighBatch
HighBatch
 
Golang, make and robotics #gocon
Golang, make and robotics #goconGolang, make and robotics #gocon
Golang, make and robotics #gocon
 
勉強会への一歩を踏み出すために
勉強会への一歩を踏み出すために勉強会への一歩を踏み出すために
勉強会への一歩を踏み出すために
 
マイクロサービスにおけるクエリー言語について
マイクロサービスにおけるクエリー言語についてマイクロサービスにおけるクエリー言語について
マイクロサービスにおけるクエリー言語について
 
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会
2015/02/21 GDG神戸 Go on Android ハンズオン&もくもく会
 
Dockerぐらし!
Dockerぐらし!Dockerぐらし!
Dockerぐらし!
 
ISUCON2015 PHPで予選を戦ってみた
ISUCON2015 PHPで予選を戦ってみたISUCON2015 PHPで予選を戦ってみた
ISUCON2015 PHPで予選を戦ってみた
 
ちょっとだけさわってみる Go言語
ちょっとだけさわってみる Go言語ちょっとだけさわってみる Go言語
ちょっとだけさわってみる Go言語
 

Similar to Gunosy Go lang study #6 net http url

Secrets of Izna
Secrets of IznaSecrets of Izna
Secrets of Iznanyaxt
 
Go言語で作る webアプリ@gocon 2013 spring
Go言語で作る webアプリ@gocon 2013 springGo言語で作る webアプリ@gocon 2013 spring
Go言語で作る webアプリ@gocon 2013 springTakuya Ueda
 
ヒカルのGo 資料 Webアプリケーションの作り方
ヒカルのGo 資料 Webアプリケーションの作り方ヒカルのGo 資料 Webアプリケーションの作り方
ヒカルのGo 資料 Webアプリケーションの作り方Yosuke Furukawa
 
勉強会force#4 Chatter Integration
勉強会force#4 Chatter Integration勉強会force#4 Chatter Integration
勉強会force#4 Chatter IntegrationKazuki Nakajima
 
OSSから学ぶSwift実践テクニック
OSSから学ぶSwift実践テクニックOSSから学ぶSwift実践テクニック
OSSから学ぶSwift実践テクニック庸介 高橋
 
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyoto
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyotoGo言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyoto
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyotoShoot Morii
 
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃん
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃんRetrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃん
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃんYukari Sakurai
 
KituraとサーバーサイドSwift
KituraとサーバーサイドSwiftKituraとサーバーサイドSwift
KituraとサーバーサイドSwiftYUSUKE MORIZUMI
 
社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~ 社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~ Daisuke Ikeda
 
Wiresharkの解析プラグインを作る ssmjp 201409
Wiresharkの解析プラグインを作る ssmjp 201409Wiresharkの解析プラグインを作る ssmjp 201409
Wiresharkの解析プラグインを作る ssmjp 201409稔 小林
 
Beginning Java EE 6 勉強会(7) #bje_study
Beginning Java EE 6 勉強会(7) #bje_studyBeginning Java EE 6 勉強会(7) #bje_study
Beginning Java EE 6 勉強会(7) #bje_studyikeyat
 
Sohu邮箱的python经验
Sohu邮箱的python经验Sohu邮箱的python经验
Sohu邮箱的python经验Ryan Poy
 
Raspberry Pi 2 誤自宅サーバー移行日記
Raspberry Pi 2 誤自宅サーバー移行日記Raspberry Pi 2 誤自宅サーバー移行日記
Raspberry Pi 2 誤自宅サーバー移行日記96smcln
 

Similar to Gunosy Go lang study #6 net http url (20)

Secrets of Izna
Secrets of IznaSecrets of Izna
Secrets of Izna
 
Go言語で作る webアプリ@gocon 2013 spring
Go言語で作る webアプリ@gocon 2013 springGo言語で作る webアプリ@gocon 2013 spring
Go言語で作る webアプリ@gocon 2013 spring
 
ヒカルのGo 資料 Webアプリケーションの作り方
ヒカルのGo 資料 Webアプリケーションの作り方ヒカルのGo 資料 Webアプリケーションの作り方
ヒカルのGo 資料 Webアプリケーションの作り方
 
P2Pって何?
P2Pって何?P2Pって何?
P2Pって何?
 
Akka HTTP
Akka HTTPAkka HTTP
Akka HTTP
 
勉強会force#4 Chatter Integration
勉強会force#4 Chatter Integration勉強会force#4 Chatter Integration
勉強会force#4 Chatter Integration
 
HTTP2入門
HTTP2入門HTTP2入門
HTTP2入門
 
OSSから学ぶSwift実践テクニック
OSSから学ぶSwift実践テクニックOSSから学ぶSwift実践テクニック
OSSから学ぶSwift実践テクニック
 
Android bluetooth
Android bluetoothAndroid bluetooth
Android bluetooth
 
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyoto
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyotoGo言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyoto
Go言語入門者が Webアプリケーション を作ってみた話 #devfest #gdgkyoto
 
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃん
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃんRetrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃん
Retrofit2 &OkHttp 
でAndroidのHTTP通信が快適だにゃん
 
Aerospike deep dive LDTs
Aerospike deep dive LDTsAerospike deep dive LDTs
Aerospike deep dive LDTs
 
KituraとサーバーサイドSwift
KituraとサーバーサイドSwiftKituraとサーバーサイドSwift
KituraとサーバーサイドSwift
 
Boost tour 1_44_0
Boost tour 1_44_0Boost tour 1_44_0
Boost tour 1_44_0
 
社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~ 社内向けTech Talk資料~Fluentdの基本紹介~
社内向けTech Talk資料~Fluentdの基本紹介~
 
Wiresharkの解析プラグインを作る ssmjp 201409
Wiresharkの解析プラグインを作る ssmjp 201409Wiresharkの解析プラグインを作る ssmjp 201409
Wiresharkの解析プラグインを作る ssmjp 201409
 
Tottoruby 20110903
Tottoruby 20110903Tottoruby 20110903
Tottoruby 20110903
 
Beginning Java EE 6 勉強会(7) #bje_study
Beginning Java EE 6 勉強会(7) #bje_studyBeginning Java EE 6 勉強会(7) #bje_study
Beginning Java EE 6 勉強会(7) #bje_study
 
Sohu邮箱的python经验
Sohu邮箱的python经验Sohu邮箱的python经验
Sohu邮箱的python经验
 
Raspberry Pi 2 誤自宅サーバー移行日記
Raspberry Pi 2 誤自宅サーバー移行日記Raspberry Pi 2 誤自宅サーバー移行日記
Raspberry Pi 2 誤自宅サーバー移行日記
 

Recently uploaded

TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdf
TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdfTaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdf
TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdfMatsushita Laboratory
 
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-LoopへTetsuya Nihonmatsu
 
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法ssuser370dd7
 
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~arts yokohama
 
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦Sadao Tokuyama
 
2024 01 Virtual_Counselor
2024 01 Virtual_Counselor 2024 01 Virtual_Counselor
2024 01 Virtual_Counselor arts yokohama
 
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...yoshidakids7
 
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也harmonylab
 
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見Shumpei Kishi
 
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)ssuser539845
 

Recently uploaded (13)

What is the world where you can make your own semiconductors?
What is the world where you can make your own semiconductors?What is the world where you can make your own semiconductors?
What is the world where you can make your own semiconductors?
 
TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdf
TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdfTaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdf
TaketoFujikawa_台本中の動作表現に基づくアニメーション原画システムの提案_SIGEC71.pdf
 
2024 03 CTEA
2024 03 CTEA2024 03 CTEA
2024 03 CTEA
 
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ
「今からでも間に合う」GPTsによる 活用LT会 - 人とAIが協調するHumani-in-the-Loopへ
 
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法
情報処理学会86回全国大会_Generic OAMをDeep Learning技術によって実現するための課題と解決方法
 
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~
2024 02 Nihon-Tanken ~Towards a More Inclusive Japan~
 
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦
ARスタートアップOnePlanetの Apple Vision Proへの情熱と挑戦
 
2024 01 Virtual_Counselor
2024 01 Virtual_Counselor 2024 01 Virtual_Counselor
2024 01 Virtual_Counselor
 
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...
Summary of "ChatDoctor: A Medical Chat Model Fine-Tuned on a Large Language M...
 
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也
【修士論文】代替出勤者の選定業務における依頼順決定方法に関する研究   千坂知也
 
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見
持続可能なDrupal Meetupのコツ - Drupal Meetup Tokyoの知見
 
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)
IFIP IP3での資格制度を対象とする国際認定(IPSJ86全国大会シンポジウム)
 
2024 04 minnanoito
2024 04 minnanoito2024 04 minnanoito
2024 04 minnanoito
 

Gunosy Go lang study #6 net http url

  • 2. 自己紹介 • 印南 聡志 (いんなみ さとし) • 6月にGunosyに入社 • https://github.com/satoshi03 • Java/Ruby … 最近 Python/Go 始めました
  • 3. Package net/http + net/url • net • net/http – cgi – cookiejar – httptest – httputil – pprof • net/url 物理層 データリンク層 ネットワーク層 (IP) トランスポート層(TCP/UDP) アプリケーション層 (HTTP)
  • 4. Package net • Package net provides a portable interface for network I/O, including TCP/IP, UDP, domain name resolution, and Unix domain sockets. • 比較的低レイヤーの通信を実現 物理層 データリンク層 ネットワーク層 (IP) トランスポート層(TCP/UDP) アプリケーション層 (HTTP)
  • 6. net server • func Listen(network, laddr string) (Listener, error) – クライアントからのコネクションするための接続口を作成 – laddr : ローカルアドレス – network : “tcp”, “tcp4”, “tcp6”, “unix”, “unixpacket” • 指定可能 : stream orientedなもの • それ以外 : エラー – Example : • Listen(“tcp” “:8080”)
  • 7. net client • func Dial(network, address string) (Conn, error) – 指定したアドレスにnetworkで指定した方法(≒Protocol)で 接続 – Network に指定する値 • tcp, tcp4, tcp6m udp, udp4, udp6, ip, ip4, ip6, unix, unixgram, unixpacket – Examples • Dial(“tcp”, “12.34.56.78:80”) • Dial(“tcp”, “google.com:http”) • Dial(“tcp”, “[2001:db8::1]:http”) • Dial(“tcp”, “[fe80::1%lo0]:80”)
  • 8. net server • Func Accept(network, address string) (Conn, error) – クライアントからの接続を受諾し、Connオブジェクトを返信 – network : “tcp”, “tcp4”, “tcp6”, “unix”, “unixpacket” • 指定可能 : stream orientedなもの • それ以外 : エラー
  • 10. Package http • Package http provides HTTP client and server implementations. 物理層 データリンク層 ネットワーク層 (IP) トランスポート層(TCP/UDP) アプリケーション層 (HTTP)
  • 12. http server • type Server type Server struct { Addr string Handler Handler ReadTimeout time.Duration WriteTimeout time.Duration MaxHeaderBytes int TLSConfig *tls.Confis }
  • 13. http server func ListenAndServe(addr string, handler Handler) error – 指定したアドレス、ハンドラーを使用してサーバー を起動 type Handler interface { ServeHTTP(ResponseWriter, *Request) }
  • 14. http server demo type AppHandler struct { } func(index *AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, »hello world ») } func main() { index := new(AppHandler) http.ListenAndServe(":8080", index) }
  • 15. Package http http.Server http.Handler http.Handler http.Handler http.Client ListenAndServe :80 GET/POST/HEAD http.ServeMux
  • 16. http server • func Handle(pattern string, handler Handler) – 指定したパターンでハンドラーを追加 • func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) – 指定したパターンでファンクションハンドラーを追加
  • 17. http server demo type IndexHandler struct { } func(index *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "This is index page.”) } type DetailHandler struct { } func(index *DetailHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "This is detail page.")↲ } func main() { index := new(IndexHandler)↲ detail := new(DetailHandler)↲ http.Handle("/", index) http.Handle("/detail", detail) http.ListenAndServe(":8080", nil) }
  • 18. http client (1/3) • func (c *Client) Get(url string) (resp *Response, err error) – 指定したURLに対してGETリクエストを送信してレスポンス を取得 – url : URL文字列
  • 19. http client(2/3) • func (c *Client) Post(url string, bodyType string, body io.Reader) (resp *Response, err error) – 指定したURLに対してPOSTリクエストを送信 – url : url文字列 – bodyType : post時に送信するバイト列の種類 – body : 送信するバイト列 – Example: • resp, err := http.Post(“http://example.com/upload", "image/jpeg", &buf)
  • 20. http client(3/3) • func (c *Client) PostForm(url string, data url.Values) (resp *Response, err error) – Key Value形式でPOSTリクエストを送信 – url : url文字列 – data : key value形式のデータ • Example – resp, err := http.PostForm("http://example.com/form", url.Values{"key": {"Value"}, "id": {"123"}})
  • 21. Package url • URLを管理するパッケージ – URLのパース – クエリーのエスケープ処理
  • 22. type url type URL struct { Scheme string Opaque string // encoded opaque data User *Userinfo // username and password information Host string // host or host:port Path string RawQuery string // encoded query values, without '?' Fragment string // fragment for references, without '#' } scheme://[userinfo@]host/path[?query][#fragment]
  • 23. url • func Parse(rawurl string) (url *URL, err error) – URL文字列をパースしてURLオブジェクトを取得 u, err := url.Parse("http://bing.com/search?q=dotnet") if err != nil { log.Fatal(err) } u.Scheme = "https” u.Host = "google.com” q := u.Query() q.Set("q", "golang") u.RawQuery = q.Encode() fmt.Println(u) > https://google.com/search?q=golang http://play.golang.org/p/8Id1F0vfvD
  • 24. url • QueryEscape(s string) string – クエリーの文字列のエスケープに変換 • QueryUnescape(s string) (string, error) – エスケープされたクエリーを文字列に変換 escaped_url := url.QueryEscape(http://bing.com/search?q=test) fmt.Println("escaped_url : " + escaped_url) unescaped_url, err := url.QueryUnescape(escaped_url) if err != nil { log.Fatal(err) } fmt.Println("escaped_url : " + unescaped_url) escaped_url : http%3A%2F%2Fbing.com%2Fsearch%3Fq%3Dtest escaped_url : http://bing.com/search?q=test http://play.golang.org/p/-jZzlqHdXm
  • 25. Package net • net • net/http – cgi – cookiejar – httptest – httputil – pprof • net/url 物理層 データリンク層 ネットワーク層 (IP) トランスポート層(TCP/UDP) アプリケーション層 (HTTP)

Editor's Notes

  1. http://play.golang.org/p/3MNm-ox8rG