In [1]:
versioninfo()
Julia Version 1.3.0
Commit 46ce4d7933 (2019-11-26 06:09 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)

HTTP.jl

↓インストール(最初に1回だけ実施)

In [ ]:
]add HTTP

GET Request Sample

In [2]:
using HTTP
In [3]:
HTTP.request("GET", "https://www.google.com/search?q=julialang&oe=UTF-8")
Out[3]:
HTTP.Messages.Response:
"""
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Fri, 13 Dec 2019 13:03:58 GMT
Expires: -1
Cache-Control: private, max-age=0
P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
Server: gws
X-XSS-Protection: 0
X-Frame-Options: SAMEORIGIN
Set-Cookie: 1P_JAR=2019-12-13-13; expires=Sun, 12-Jan-2020 13:03:58 GMT; path=/; domain=.google.com
Set-Cookie: CGIC=; expires=Wed, 10-Jun-2020 13:03:58 GMT; path=/complete/search; domain=.google.com; HttpOnly
Set-Cookie: CGIC=; expires=Wed, 10-Jun-2020 13:03:58 GMT; path=/search; domain=.google.com; HttpOnly
Set-Cookie: NID=193=hJBgnbkcfmJ1cGOh6zSa2MsVHJJv6OPklqpVXTuEdpGD1OTkAOC3hf8sRsMMYNYD8W9WMMKMcCcvCkeFMFzaN6_v4qlQKNBnW8x870D-0I-U63hPy227Fc5Pta2-pKCnXAOTzInnioznCmt5HPmukXA__RFMPyzoAP53u09fS20; expires=Sat, 13-Jun-2020 13:03:58 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

<!doctype html><html lang="ja"><head><meta charset="UTF-8"><meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"><title>julialang - Google 検索</title><script nonce="g4BIzr9klAm/00g+G3gmAg==">(function(){document.documentElement.addEventListener("submit",function(b){var a;if(a=b.target){var c=a.getAttribute("data-submitfalse");a="1"==c||"q"==c&&!a.elements.q.value?!0:!1}else a=!1;a&&(b.preventDefault(),b.stopPropagation())},!0);}).call(this);(function(){var a=window.performance;window.start=(new Date).getTime();a:{var b=window;if(a){var c=a.timing;if(c){var d=c.navigationStart,e=c.responseStart;if(e>d&&e<=window.start){window.start=e;b.wsrt=e-d;break a}}a.now&&(b.wsrt=Math.floor(a.now()))}}window.google=window.google||{};google.aft=function(f){f.setAttribute("data-iml",+new Date)};}).call(this);(function(){window.jsarwt=function(){return!1};}).call(this);(function(){var c=[],e=0;window.ping=function(b){-1==b.indexOf("&zx")&&(b+="&zx="+(new Date
⋮
73480-byte body
"""

Asynchronus Request Sample

In [4]:
function do_search(results::Channel{Tuple{String, Float64, HTTP.Messages.Response}}, engine::String, url::String)
    res, exec_time = @timed HTTP.request("GET", url)
    if isopen(results)
        put!(results, (engine, exec_time, res))
    end
end
Out[4]:
do_search (generic function with 1 method)
In [5]:
function print_response(engine, exec_time, response)
    println("engine: ", engine)
    println("exec_time: ", exec_time)
    print("response: (")
    print("$(response.status) $(HTTP.Messages.STATUS_MESSAGES[response.status]), ")
    print("$(length(response.headers)) headers, ")
    println("$(length(response.body)) bytes in body)")
end
Out[5]:
print_response (generic function with 1 method)
In [6]:
precompile(do_search, (Channel{Tuple{String, Float64, HTTP.Messages.Response}}, String, String))
precompile(print_response, (String, Float64, HTTP.Messages.Response))
Out[6]:
true
In [7]:
channel = Channel{Tuple{String, Float64, HTTP.Messages.Response}}(1)
@async do_search(channel, "Google", "https://www.google.com/search?q=julialang&oe=UTF-8")
res0 = take!(channel)
print_response(res0...)
close(channel)
engine: Google
exec_time: 0.487161003
response: (200 OK, 16 headers, 73480 bytes in body)

早いもの勝ち

In [8]:
function main()
    results = Channel{Tuple{String, Float64, HTTP.Messages.Response}}(3)
    
    searches = [
        "Google"=>"https://www.google.com/search?q=julialang&oe=UTF-8", 
        "Bing"=>"https://www.bing.com/search?q=julialang",
        "DuckDuckGo"=>"https://duckduckgo.com/?q=julialang"]
    for (engine, url) = searches
        @async do_search(results, engine, url)
    end

    res0 = take!(results)
    close(results)
    
    print_response(res0...)
end
Out[8]:
main (generic function with 1 method)
In [9]:
precompile(main, ())
Out[9]:
true
In [10]:
main()
engine: Google
exec_time: 0.435377601
response: (200 OK, 16 headers, 73474 bytes in body)
In [11]:
main()
engine: DuckDuckGo
exec_time: 0.25063164
response: (200 OK, 18 headers, 19049 bytes in body)
In [12]:
main()
engine: DuckDuckGo
exec_time: 0.287571225
response: (200 OK, 18 headers, 19049 bytes in body)
In [13]:
main()
engine: DuckDuckGo
exec_time: 0.287246897
response: (200 OK, 18 headers, 19049 bytes in body)
In [14]:
main()
engine: DuckDuckGo
exec_time: 0.289991983
response: (200 OK, 18 headers, 19049 bytes in body)

Search Race

In [15]:
function main2()
    results = Channel{Tuple{String, Float64, HTTP.Messages.Response}}(3)
    
    searches = [
        "Google"=>"https://www.google.com/search?q=julialang&oe=UTF-8", 
        "Bing"=>"https://www.bing.com/search?q=julialang",
        "DuckDuckGo"=>"https://duckduckgo.com/?q=julialang"]
    for (engine, url) = searches
        @async do_search(results, engine, url)
    end

    res1 = take!(results)
    print_response(res1...)
    println()
    
    res2 = take!(results)
    print_response(res2...)
    println()
    
    res3 = take!(results)
    print_response(res3...)
    
    close(results)
end
Out[15]:
main2 (generic function with 1 method)
In [16]:
precompile(main2, ())
Out[16]:
true
In [17]:
main2()
engine: DuckDuckGo
exec_time: 0.360540393
response: (200 OK, 18 headers, 19049 bytes in body)

engine: Bing
exec_time: 0.429006315
response: (200 OK, 16 headers, 158713 bytes in body)

engine: Google
exec_time: 0.508506785
response: (200 OK, 16 headers, 73474 bytes in body)
In [18]:
main2()
engine: DuckDuckGo
exec_time: 0.290471476
response: (200 OK, 18 headers, 19049 bytes in body)

engine: Bing
exec_time: 0.374741239
response: (200 OK, 16 headers, 157068 bytes in body)

engine: Google
exec_time: 0.493652595
response: (200 OK, 16 headers, 73480 bytes in body)
In [ ]: