メモ:Rでtelnet的なことをしたいときはsocketConnection()にcat()する

telnet的なことをしたいときはsocketConnection()でコネクションをつくってそこにcat()とかwriteLines()とかすればいいらしい。以下はテキストでHTTPリクエストを投げる例。

con <- socketConnection("example.com", port = 80)

req <- "GET / HTTP/1.1
Host: example.com

"

cat(req, file = con)
readLines(con)
#>  [1] "HTTP/1.1 200 OK"                                                                                      
#>  [2] "Accept-Ranges: bytes"                                                                                 
#>  [3] "Cache-Control: max-age=604800"                                                                        
#>  [4] "Content-Type: text/html"                                                                              
#>  [5] "Date: Sat, 17 Sep 2016 10:34:30 GMT"                                                                  
#>  [6] "Etag: \"359670651\""                                                                                  
#>  [7] "Expires: Sat, 24 Sep 2016 10:34:30 GMT"                                                               
#>  [8] "Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT"                                                         
#>  [9] "Server: ECS (pae/3796)"                                                                               
#> [10] "Vary: Accept-Encoding"                                                                                
#> [11] "X-Cache: HIT"                                                                                         
#> [12] "x-ec-custom-error: 1"                                                                                 
#> [13] "Content-Length: 1270"                                                                                 
#> [14] ""                                                                                                     
#> [15] "<!doctype html>"                                                                                      
#> [16] "<html>"                                                                                               
#> [17] "<head>"                                                                                               
#> [18] "    <title>Example Domain</title>"                                                                    
#> [19] ""                                                                                                     
#> [20] "    <meta charset=\"utf-8\" />"                                                                       
#> [21] "    <meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\" />"                        
#> [22] "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />"                       
#> [23] "    <style type=\"text/css\">"                                                                        
#> [24] "    body {"                                                                                           
#> [25] "        background-color: #f0f0f2;"                                                                   
#> [26] "        margin: 0;"                                                                                   
#> [27] "        padding: 0;"                                                                                  
#> [28] "        font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;"                
#> [29] "        "                                                                                             
#> [30] "    }"                                                                                                
#> [31] "    div {"                                                                                            
#> [32] "        width: 600px;"                                                                                
#> [33] "        margin: 5em auto;"                                                                            
#> [34] "        padding: 50px;"                                                                               
#> [35] "        background-color: #fff;"                                                                      
#> [36] "        border-radius: 1em;"                                                                          
#> [37] "    }"                                                                                                
#> [38] "    a:link, a:visited {"                                                                              
#> [39] "        color: #38488f;"                                                                              
#> [40] "        text-decoration: none;"                                                                       
#> [41] "    }"                                                                                                
#> [42] "    @media (max-width: 700px) {"                                                                      
#> [43] "        body {"                                                                                       
#> [44] "            background-color: #fff;"                                                                  
#> [45] "        }"                                                                                            
#> [46] "        div {"                                                                                        
#> [47] "            width: auto;"                                                                             
#> [48] "            margin: 0 auto;"                                                                          
#> [49] "            border-radius: 0;"                                                                        
#> [50] "            padding: 1em;"                                                                            
#> [51] "        }"                                                                                            
#> [52] "    }"                                                                                                
#> [53] "    </style>    "                                                                                     
#> [54] "</head>"                                                                                              
#> [55] ""                                                                                                     
#> [56] "<body>"                                                                                               
#> [57] "<div>"                                                                                                
#> [58] "    <h1>Example Domain</h1>"                                                                          
#> [59] "    <p>This domain is established to be used for illustrative examples in documents. You may use this"
#> [60] "    domain in examples without prior coordination or asking for permission.</p>"                      
#> [61] "    <p><a href=\"http://www.iana.org/domains/example\">More information...</a></p>"                   
#> [62] "</div>"                                                                                               
#> [63] "</body>"                                                                                              
#> [64] "</html>"                                                                                              

close(con)