R : Copyright 2003, The R Development Core Team Version 1.7.0 Under development (unstable) (2003-03-07) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type `license()' or `licence()' for distribution details. R is a collaborative project with many contributors. Type `contributors()' for more information. Type `demo()' for some demos, `help()' for on-line help, or `help.start()' for a HTML browser interface to help. Type `q()' to quit R. > ## These are tests that require socket and internet functionality, and > ## a working Internet connection. > ## We attempt to test for those. > > if(!capabilities()["http/ftp"]) { + warning("no internet capabilities") + q() + } > > if(.Platform$OS.type == "unix" && + is.null(nsl("cran.r-project.org"))) q() > > # test do_download. > nrow(CRAN.packages()) trying URL `http://cran.r-project.org/src/contrib/PACKAGES' Content type `text/plain; charset=iso-8859-1' length 99193 bytes opened URL .......... .......... .......... .......... .......... .......... .......... .......... .......... ...... downloaded 96Kb [1] 202 > > # test url connections on http > zz <- url("http://cran.r-project.org/") > readLines(zz) [1] "" [2] "" [3] "" [4] "" [5] "The Comprehensive R Archive Network" [6] "" [7] "" [8] "" [9] "" [10] "" [11] "" [12] "" [13] "" [14] "" [15] "" [16] "" [17] "" [18] "<h1>The Comprehensive R Archive Network</h1>" [19] "" [20] "Your browser seems not to support frames," [21] "here is the <A href=\"navbar.html\">contents page</A> of CRAN." [22] "" [23] "" [24] "" [25] "" [26] "" > close(zz) > > ## check graceful failure: > try(zz <- url("http://foo.bar", "r")) unable to resolve 'foo.bar'. Error in url("http://foo.bar", "r") : cannot open URL `http://foo.bar' > > # and via read.table, test http and ftp. > > read.table("http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat") V1 V2 V3 V4 V5 1 1 307 930 36.58 0 2 2 307 940 36.73 0 3 3 307 950 36.93 0 4 4 307 1000 37.15 0 5 5 307 1010 37.23 0 6 6 307 1020 37.24 0 7 7 307 1030 37.24 0 8 8 307 1040 36.90 0 9 9 307 1050 36.95 0 10 10 307 1100 36.89 0 11 11 307 1110 36.95 0 12 12 307 1120 37.00 0 13 13 307 1130 36.90 0 14 14 307 1140 36.99 0 15 15 307 1150 36.99 0 16 16 307 1200 37.01 0 17 17 307 1210 37.04 0 18 18 307 1220 37.04 0 19 19 307 1230 37.14 0 20 20 307 1240 37.07 0 21 21 307 1250 36.98 0 22 22 307 1300 37.01 0 23 23 307 1310 36.97 0 24 24 307 1320 36.97 0 25 25 307 1330 37.12 0 26 26 307 1340 37.13 0 27 27 307 1350 37.14 0 28 28 307 1400 37.15 0 29 29 307 1410 37.17 0 30 30 307 1420 37.12 0 31 31 307 1430 37.12 0 32 32 307 1440 37.17 0 33 33 307 1450 37.28 0 34 34 307 1500 37.28 0 35 35 307 1510 37.44 0 36 36 307 1520 37.51 0 37 37 307 1530 37.64 0 38 38 307 1540 37.51 0 39 39 307 1550 37.98 1 40 40 307 1600 38.02 1 41 41 307 1610 38.00 1 42 42 307 1620 38.24 1 43 43 307 1630 38.10 1 44 44 307 1640 38.24 1 45 45 307 1650 38.11 1 46 46 307 1700 38.02 1 47 47 307 1710 38.11 1 48 48 307 1720 38.01 1 49 49 307 1730 37.91 1 50 50 307 1740 37.96 1 51 51 307 1750 38.03 1 52 52 307 1800 38.17 1 53 53 307 1810 38.19 1 54 54 307 1820 38.18 1 55 55 307 1830 38.15 1 56 56 307 1840 38.04 1 57 57 307 1850 37.96 1 58 58 307 1900 37.84 1 59 59 307 1910 37.83 1 60 60 307 1920 37.84 1 61 61 307 1930 37.74 1 62 62 307 1940 37.76 1 63 63 307 1950 37.76 1 64 64 307 2000 37.64 1 65 65 307 2010 37.63 1 66 66 307 2020 38.06 1 67 67 307 2030 38.19 1 68 68 307 2040 38.35 1 69 69 307 2050 38.25 1 70 70 307 2100 37.86 1 71 71 307 2110 37.95 1 72 72 307 2120 37.95 1 73 73 307 2130 37.76 1 74 74 307 2140 37.60 1 75 75 307 2150 37.89 1 76 76 307 2200 37.86 1 77 77 307 2210 37.71 1 78 78 307 2220 37.78 1 79 79 307 2230 37.82 1 80 80 307 2240 37.76 1 81 81 307 2250 37.81 1 82 82 307 2300 37.84 1 83 83 307 2310 38.01 1 84 84 307 2320 38.10 1 85 85 307 2330 38.15 1 86 86 307 2340 37.92 1 87 87 307 2350 37.64 1 88 88 308 0 37.70 1 89 89 308 10 37.46 1 90 90 308 20 37.41 1 91 91 308 30 37.46 1 92 92 308 40 37.56 1 93 93 308 50 37.55 1 94 94 308 100 37.75 1 95 95 308 110 37.76 1 96 96 308 120 37.73 1 97 97 308 130 37.77 1 98 98 308 140 38.01 1 99 99 308 150 38.04 1 100 100 308 200 38.07 1 > read.table("ftp://ftp.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat") V1 V2 V3 V4 V5 1 1 307 930 36.58 0 2 2 307 940 36.73 0 3 3 307 950 36.93 0 4 4 307 1000 37.15 0 5 5 307 1010 37.23 0 6 6 307 1020 37.24 0 7 7 307 1030 37.24 0 8 8 307 1040 36.90 0 9 9 307 1050 36.95 0 10 10 307 1100 36.89 0 11 11 307 1110 36.95 0 12 12 307 1120 37.00 0 13 13 307 1130 36.90 0 14 14 307 1140 36.99 0 15 15 307 1150 36.99 0 16 16 307 1200 37.01 0 17 17 307 1210 37.04 0 18 18 307 1220 37.04 0 19 19 307 1230 37.14 0 20 20 307 1240 37.07 0 21 21 307 1250 36.98 0 22 22 307 1300 37.01 0 23 23 307 1310 36.97 0 24 24 307 1320 36.97 0 25 25 307 1330 37.12 0 26 26 307 1340 37.13 0 27 27 307 1350 37.14 0 28 28 307 1400 37.15 0 29 29 307 1410 37.17 0 30 30 307 1420 37.12 0 31 31 307 1430 37.12 0 32 32 307 1440 37.17 0 33 33 307 1450 37.28 0 34 34 307 1500 37.28 0 35 35 307 1510 37.44 0 36 36 307 1520 37.51 0 37 37 307 1530 37.64 0 38 38 307 1540 37.51 0 39 39 307 1550 37.98 1 40 40 307 1600 38.02 1 41 41 307 1610 38.00 1 42 42 307 1620 38.24 1 43 43 307 1630 38.10 1 44 44 307 1640 38.24 1 45 45 307 1650 38.11 1 46 46 307 1700 38.02 1 47 47 307 1710 38.11 1 48 48 307 1720 38.01 1 49 49 307 1730 37.91 1 50 50 307 1740 37.96 1 51 51 307 1750 38.03 1 52 52 307 1800 38.17 1 53 53 307 1810 38.19 1 54 54 307 1820 38.18 1 55 55 307 1830 38.15 1 56 56 307 1840 38.04 1 57 57 307 1850 37.96 1 58 58 307 1900 37.84 1 59 59 307 1910 37.83 1 60 60 307 1920 37.84 1 61 61 307 1930 37.74 1 62 62 307 1940 37.76 1 63 63 307 1950 37.76 1 64 64 307 2000 37.64 1 65 65 307 2010 37.63 1 66 66 307 2020 38.06 1 67 67 307 2030 38.19 1 68 68 307 2040 38.35 1 69 69 307 2050 38.25 1 70 70 307 2100 37.86 1 71 71 307 2110 37.95 1 72 72 307 2120 37.95 1 73 73 307 2130 37.76 1 74 74 307 2140 37.60 1 75 75 307 2150 37.89 1 76 76 307 2200 37.86 1 77 77 307 2210 37.71 1 78 78 307 2220 37.78 1 79 79 307 2230 37.82 1 80 80 307 2240 37.76 1 81 81 307 2250 37.81 1 82 82 307 2300 37.84 1 83 83 307 2310 38.01 1 84 84 307 2320 38.10 1 85 85 307 2330 38.15 1 86 86 307 2340 37.92 1 87 87 307 2350 37.64 1 88 88 308 0 37.70 1 89 89 308 10 37.46 1 90 90 308 20 37.41 1 91 91 308 30 37.46 1 92 92 308 40 37.56 1 93 93 308 50 37.55 1 94 94 308 100 37.75 1 95 95 308 110 37.76 1 96 96 308 120 37.73 1 97 97 308 130 37.77 1 98 98 308 140 38.01 1 99 99 308 150 38.04 1 100 100 308 200 38.07 1 > > > if(!capabilities()["sockets"]) stop("no socket capabilities") > > # do the same thing via sockets (cut-down httpclient) > httpget <- function (url, port = 80) + { + urlel <- strsplit(url, "/")[[1]] + if (urlel[1] != "http:") stop("Not an http:// URL") + host <- urlel[3] + rurl <- paste(c("", urlel[-(1:3)]), collapse = "/") + a <- make.socket(host, port = port) + on.exit(close.socket(a)) + headreq <- paste("HEAD", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n") + write.socket(a, headreq) + head <- read.socket(a, maxlen = 8000) + b <- strsplit(head, "\n")[[1]] + if (length(grep("200 OK", b[1])) == 0) stop(b[1]) + len <- as.numeric(strsplit(grep("Content-Length", b, value = TRUE), + ":")[[1]][2]) + getreq <- paste("GET", rurl, "HTTP/1.0\r\nConnection: Keep-Alive\r\nAccept: text/plain\r\n\r\n") + write.socket(a, getreq) + junk <- read.socket(a, maxlen = nchar(head)) + data <- "" + b <- strsplit(c(head, junk), "\n") + nn <- length(b[[1]]) + if (length(b[[2]]) > nn) + data <- paste(b[[2]][-(1:nn)], collapse = "\n") + while (nchar(data) < len) { + data <- paste(data, read.socket(a, maxlen = len - nchar(data)), + sep = "") + } + strsplit(data, "\n")[[1]] + } > httpget("http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat") [1] "001 307 0930 36.58 0" "002 307 0940 36.73 0" "003 307 0950 36.93 0" [4] "004 307 1000 37.15 0" "005 307 1010 37.23 0" "006 307 1020 37.24 0" [7] "007 307 1030 37.24 0" "008 307 1040 36.90 0" "009 307 1050 36.95 0" [10] "010 307 1100 36.89 0" "011 307 1110 36.95 0" "012 307 1120 37.00 0" [13] "013 307 1130 36.90 0" "014 307 1140 36.99 0" "015 307 1150 36.99 0" [16] "016 307 1200 37.01 0" "017 307 1210 37.04 0" "018 307 1220 37.04 0" [19] "019 307 1230 37.14 0" "020 307 1240 37.07 0" "021 307 1250 36.98 0" [22] "022 307 1300 37.01 0" "023 307 1310 36.97 0" "024 307 1320 36.97 0" [25] "025 307 1330 37.12 0" "026 307 1340 37.13 0" "027 307 1350 37.14 0" [28] "028 307 1400 37.15 0" "029 307 1410 37.17 0" "030 307 1420 37.12 0" [31] "031 307 1430 37.12 0" "032 307 1440 37.17 0" "033 307 1450 37.28 0" [34] "034 307 1500 37.28 0" "035 307 1510 37.44 0" "036 307 1520 37.51 0" [37] "037 307 1530 37.64 0" "038 307 1540 37.51 0" "039 307 1550 37.98 1" [40] "040 307 1600 38.02 1" "041 307 1610 38.00 1 " "042 307 1620 38.24 1 " [43] "043 307 1630 38.10 1" "044 307 1640 38.24 1" "045 307 1650 38.11 1" [46] "046 307 1700 38.02 1" "047 307 1710 38.11 1" "048 307 1720 38.01 1" [49] "049 307 1730 37.91 1" "050 307 1740 37.96 1" "051 307 1750 38.03 1" [52] "052 307 1800 38.17 1" "053 307 1810 38.19 1" "054 307 1820 38.18 1" [55] "055 307 1830 38.15 1" "056 307 1840 38.04 1" "057 307 1850 37.96 1" [58] "058 307 1900 37.84 1" "059 307 1910 37.83 1" "060 307 1920 37.84 1" [61] "061 307 1930 37.74 1" "062 307 1940 37.76 1" "063 307 1950 37.76 1" [64] "064 307 2000 37.64 1" "065 307 2010 37.63 1" "066 307 2020 38.06 1" [67] "067 307 2030 38.19 1" "068 307 2040 38.35 1" "069 307 2050 38.25 1" [70] "070 307 2100 37.86 1" "071 307 2110 37.95 1" "072 307 2120 37.95 1" [73] "073 307 2130 37.76 1" "074 307 2140 37.60 1" "075 307 2150 37.89 1" [76] "076 307 2200 37.86 1" "077 307 2210 37.71 1" "078 307 2220 37.78 1" [79] "079 307 2230 37.82 1" "080 307 2240 37.76 1" "081 307 2250 37.81 1" [82] "082 307 2300 37.84 1" "083 307 2310 38.01 1" "084 307 2320 38.10 1" [85] "085 307 2330 38.15 1" "086 307 2340 37.92 1" "087 307 2350 37.64 1" [88] "088 308 0000 37.70 1" "089 308 0010 37.46 1" "090 308 0020 37.41 1" [91] "091 308 0030 37.46 1" "092 308 0040 37.56 1" "093 308 0050 37.55 1" [94] "094 308 0100 37.75 1" "095 308 0110 37.76 1" "096 308 0120 37.73 1" [97] "097 308 0130 37.77 1" "098 308 0140 38.01 1" "099 308 0150 38.04 1" [100] "100 308 0200 38.07 1" > > finger <- function(user, host = "localhost", port = 79, print = TRUE) + { + if (!is.character(user)) + stop("user name must be a string") + user <- paste(user,"\r\n") + socket <- make.socket(host, port) + on.exit(close.socket(socket)) + write.socket(socket, user) + output <- character(0) + repeat{ + ss <- read.socket(socket) + if (ss == "") break + output <- paste(output, ss) + } + close.socket(socket) + if (print) cat(output) + invisible(output) + } > try(finger("root")) ## only works if your site provides a finger daemon Error in make.socket(host, port) : Socket not established >