Mac で Julia #5 Networking and Streams


前回に引き続きJuliaについてです。

[ドキュメント](https://docs.julialang.org/)に沿って試していきます。
...といいつついきなり飛びましたw


## Working with Files

```julia
julia> f = open("hello.txt")
IOStream(<file hello.txt>)

julia> readlines(f)
2-element Array{String,1}:
 "Hello World"
 "Thanks"

julia> close(f)
```

テキストファイルの読み込みです。

```julia
julia> f = open("hello.txt","w");

julia> write(f,"Hello\nAgain");

julia> close(f)
```

書き込みです。

モード切り替えは下記のようになってます。

|Mode|Description|Keywords|
|:--:|:--:|:--:|
|r	|read	|none
|w	|write, create, truncate|write = true
|a	|write, create, append|append = true
|r+	|read, write	|read = true, write = true
|w+	|read, write, create, truncate	|truncate = true, read = true
|a+	|read, write, create, append|append = true, read = true


### HTTP Connection

- 徐々に応用編も入れてきたいとおもいます。ファイルではなく通信で情報を取得します。

```julia
julia> import Pkg

julia> Pkg.update()
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
  Updating `~/.julia/environments/v1.0/Project.toml`
 [no changes]
  Updating `~/.julia/environments/v1.0/Manifest.toml`
 [no changes]

julia> Pkg.add("HTTP")
 Resolving package versions...
 Installed IniFile ─ v0.5.0
 Installed HTTP ──── v0.6.14
  Updating `~/.julia/environments/v1.0/Project.toml`
  [cd3eb016] + HTTP v0.6.14
  Updating `~/.julia/environments/v1.0/Manifest.toml`
  [cd3eb016] + HTTP v0.6.14
  [83e8ac13] + IniFile v0.5.0

julia> import HTTP
[ Info: Precompiling HTTP [cd3eb016-35fb-5094-929b-558a96fad6f3]

julia> r = HTTP.request("GET" , "https://julialang.org/");

julia> r.status
200

julia> r.headers
23-element Array{Pair{SubString{String},SubString{String}},1}:
                      "Server" => "GitHub.com"
                "Content-Type" => "text/html; charset=utf-8"
               "Last-Modified" => "Wed, 22 Aug 2018 14:24:05 GMT"
                        "ETag" => "\"5b7d7205-8cc5\""
 "Access-Control-Allow-Origin" => "*"
                     "Expires" => "Wed, 22 Aug 2018 17:02:01 GMT"
               "Cache-Control" => "max-age=600"
         "X-GitHub-Request-Id" => "7FBA:5D75:D2978:E7DA7:5B7D94B1"
                         "Via" => "1.1 varnish"
         "Fastly-Debug-Digest" => "4def1731cf2d863872114215d59066401d557c8a778ad2239a9087e4ac5a2b2a"
                               ⋮
                        "Date" => "Thu, 23 Aug 2018 06:28:12 GMT"
                         "Via" => "1.1 varnish"
                         "Age" => "0"
                  "Connection" => "keep-alive"
                 "X-Served-By" => "cache-nrt6126-NRT, cache-nrt6136-NRT"
                     "X-Cache" => "MISS, MISS"
                "X-Cache-Hits" => "0, 0"
                     "X-Timer" => "S1535005692.856555,VS0,VE174"
                        "Vary" => "Accept-Encoding"
   "Strict-Transport-Security" => "max-age=5356800"
   
julia> String(r.body)
"<!DOCTYPE html>\n<html>\n\n<head>\n  <meta charset=\"utf-8\">\n<meta http-equiv=\"x-ua-compatible\" content=\"ie=edge\">\n<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />\n<title>The Julia Language</title>\n<meta name=\"author\" content=\"Jeff Bezanson, Stefan Ka  
〜〜〜

```

取得できますね。

```julia
julia> con = HTTP.get("https://julialang.org/");

julia> println(con)
HTTP.Messages.Response:
"""
HTTP/1.1 200 OK
Server: GitHub.com
Content-Type: text/html; charset=utf-8
Last-Modified: Wed, 22 Aug 2018 14:24:05 GMT
ETag: "5b7d7205-8cc5"
Access-Control-Allow-Origin: *
Expires: Wed, 22 Aug 2018 17:02:01 GMT
Cache-Control: max-age=600
X-GitHub-Request-Id: 7FBA:5D75:D2978:E7DA7:5B7D94B1
Via: 1.1 varnish
Fastly-Debug-Digest: 4def1731cf2d863872114215d59066401d557c8a778ad2239a9087e4ac5a2b2a
X-Fastly-Request-ID: b06b2a64f208b7b21f9ae005e6a61b9fcb5fe169
Content-Length: 36037
Accept-Ranges: bytes
Date: Thu, 23 Aug 2018 06:36:44 GMT
Via: 1.1 varnish
Age: 512
Connection: keep-alive
X-Served-By: cache-nrt6126-NRT, cache-nrt6138-NRT
X-Cache: MISS, HIT
X-Cache-Hits: 0, 2
X-Timer: S1535006204.297155,VS0,VE0
Vary: Accept-Encoding
Strict-Transport-Security: max-age=5356800

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
〜〜
```

getでもまるっと取れます。HTTP.requestの方がいいですかね。

いままでの知識をつかって、HTTPでとったstatus,headers,bodyをファイルに書き出しました。

```julia
import Pkg
import HTTP
Pkg.update()
Pkg.add("HTTP")

file = open("out.txt" , "w")

r = HTTP.request("GET" , "https://julialang.org/")

write(file , "STATUS\n")
write(file , "$(r.status)\n\n")

write(file , "HEADERS\n")
for h in r.headers
    write(file ,"$(h[1]) : $(h[2])\n")
end

write(file , r.body)
write(file , "\n")

close(file)
```

次回はJSONのパースとかしてみようかと。

今日はこの辺で。