1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
|
<html><title>Programming Ruby: The Pragmatic Programmer's Guide</title><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><STYLE TYPE="text/css"><!--
BODY { margin-left: 1in;
width: 6in;
font-family: helvetica, arial, sans-serif;
}
H1 { color: #000080;
font-family: helvetica, arial, sans-serif;
font-size: 22pt;
margin-left: 0in
}
H2 { color: #000080; font: bold x-large helvetica, sans-serif;
margin-left: 0in }
H3 { color: #000080; font: bold large helvetica, sans-serif; }
H4 { color: #000080; font: italic large helvetica, sans-serif; }
.ruby { background: #fff0f0 }
.header { color: white }
.subheader { color: #ffdddd }
.sidebar { width: 6in }
span.sans { font-family: helvetica, arial, sans-serif }
-->
</STYLE><table bgcolor="#a03030" cellpadding="3" border="0" cellspacing="0"><tr><td colspan="3"><table bgcolor="#902020" cellpadding="20"><tr><td><h1 class="header">Programming Ruby</h1><h3 class="subheader">The Pragmatic Programmer's Guide</h3></td></tr></table></td></tr><tr><td width="33%" align="left"><a class="subheader" href="lib_patterns.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="lib_windows.html">Next ></a><br></td></tr></table></head><body bgcolor="white">
<!--
Copyright (c) 2001 by Addison Wesley Longman. This
material may be distributed only subject to the terms and
conditions set forth in the Open Publication License, v1.0 or
later (the latest version is presently available at
http://www.opencontent.org/openpub/).
-->
<h1>Network and Web Libraries</h1><hr><br>
<P></P>
Ruby provides two levels of access to network services. At a low
level, you can access the basic socket support in the underlying
operating system, which allows you to implement clients and servers for
both connection-oriented and connectionless protocols. These are
documented in the next section.
<P></P>
Ruby also has libraries that provide higher-level access to
specific application-level network protocols, such as FTP,
HTTP, and so on. These are documented starting
on page 486.
<P></P>
Finally, the <code>CGI</code> libraries, documented beginning on page 501,
provide server-side developers with a
convenient interface for developing Web applications.
<h2>Socket-Level Access</h2>
<P></P>
Sockets are the endpoints of a bidirectional communications
channel. Sockets may communicate within a process, between processes
on the same machine, or between processes on different
continents. Sockets may be implemented over a number of different
channel types: Unix domain sockets, TCP, UDP, and so on. The socket
library provides specific classes for handling the common transports
as well as a generic interface for handling the rest. All
functionality in the socket library is accessible through a single
extension library. Access it using
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'socket'
</pre></td></tr></table>
<P></P>
Sockets have their own vocabulary:
<P></P>
<dl>
<dt>domain</dt><dd>
The family of protocols that will be used as the transport
mechanism. These values are constants such as <code>PF_INET</code>, <code>PF_UNIX</code>,
<code>PF_X25</code>, and so on.
</dd><dt>type</dt><dd>
The type of communications between the two endpoints, typically
<code>SOCK_STREAM</code> for connection-oriented protocols and
<code>SOCK_DGRAM</code> for connectionless protocols.
</dd><dt>protocol</dt><dd>
Typically zero, this may be used to identify a variant of a protocol
within a domain and type.
</dd><dt>hostName</dt><dd>
The identifier of a network interface:
<ul>
<li> a string, which can be a host name, a dotted-quad address, or
an IPV6 address in colon (and possibly dot) notation,
</li><li> the string ``<broadcast>'', which specifies an
<code>INADDR_BROADCAST</code> address,
</li><li> a zero-length string, which specifies <code>INADDR_ANY</code>, or
</li><li> an <code>Integer</code>, interpreted as a binary address in host byte
order.
</li></ul>
</dd><dt>port</dt><dd>
(sometimes called <b>service</b>)
Each server listens for clients calling on one or more ports.
A port may be a <code>Fixnum</code> port number, a string containing a port number, or the
name of a service.
</dd></dl>
<P></P>
Sockets are children of class <code>IO</code>. Once a socket has been
successfully opened, the conventional I/O methods may be used. However,
greater efficiency is sometimes obtained by using socket-specific
methods. As with other I/O classes, socket I/O blocks by default.
The hierarchy of the socket classes is shown in Figure
26.1 on page 475.
<P></P>
For more information on the use of sockets, see your operating system
documentation. You'll also find a comprehensive treatment in
W. Richard Stevens,
<em>Unix Network Programming, Volumes 1 and
2</em> .
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class BasicSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">IO</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#do_not_reverse_lookup">do_not_reverse_lookup</a> <a href="#do_not_reverse_lookup_eq">do_not_reverse_lookup=</a> <a href="#lookup_order">lookup_order</a> <a href="#lookup_order_eq">lookup_order=</a> <a href="#close_read"><i>close_read</i></a> <a href="#close_write"><i>close_write</i></a> <a href="#getpeername"><i>getpeername</i></a> <a href="#getsockname"><i>getsockname</i></a> <a href="#getsockopt"><i>getsockopt</i></a> <a href="#recv"><i>recv</i></a> <a href="#send"><i>send</i></a> <a href="#setsockopt"><i>setsockopt</i></a> <a href="#shutdown"><i>shutdown</i></a> <p></p><hr>
<code>BasicSocket</code> is an abstract base class for all other socket
classes.
<P></P>
This class and its subclasses often manipulate addresses using
something called a <code>struct sockaddr</code>, which is effectively
an opaque binary string.<em>[In reality, it maps onto the
underlying C-language <code>struct sockaddr</code> set of structures,
documented in the man pages and in the books by Stevens.]</em>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="do_not_reverse_lookup"><b>do_not_reverse_lookup</b></a></font></td><td bgcolor="#ffaaaa">
BasicSocket.do_not_reverse_lookup -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the value of the global reverse lookup flag. If set to
<code>true</code>, queries on remote addresses will return the
numeric address but not the host name.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="do_not_reverse_lookup_eq"><b>do_not_reverse_lookup=</b></a></font></td><td bgcolor="#ffaaaa">
BasicSocket.do_not_reverse_lookup = <code>true</code> or <code>false</code></td></tr><td></td><td>
<P></P>
Sets the global reverse lookup flag.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="lookup_order"><b>lookup_order</b></a></font></td><td bgcolor="#ffaaaa">
BasicSocket.lookup_order
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the global address lookup order, one of:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Order</b></td>
<td valign="top"><b>Families Searched</b></td>
</tr>
<tr>
<td valign="top"><code>LOOKUP_UNSP</code></td>
<td valign="top"><code>AF_UNSPEC</code></td>
</tr>
<tr>
<td valign="top"><code>LOOKUP_INET</code></td>
<td valign="top"><code>AF_INET</code>, <code>AF_INET6</code>, <code>AF_UNSPEC</code></td>
</tr>
<tr>
<td valign="top"><code>LOOKUP_INET6</code></td>
<td valign="top"><code>AF_INET6</code>, <code>AF_INET</code>, <code>AF_UNSPEC</code></td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="lookup_order_eq"><b>lookup_order=</b></a></font></td><td bgcolor="#ffaaaa">
BasicSocket.lookup_order = <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Sets the global address lookup order.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="close_read"><b>close_read</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.close_read -> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Closes the readable connection on this socket.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="close_write"><b>close_write</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.close_write -> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Closes the writable connection on this socket.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getpeername"><b>getpeername</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.getpeername
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the <code>struct sockaddr</code> structure associated with the
other end of this socket connection.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getsockname"><b>getsockname</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.getsockname
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the <code>struct sockaddr</code> structure associated with <i>aSession</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getsockopt"><b>getsockopt</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.getsockopt( <i>level</i>,
<i>optname</i> ) -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the value of the specified option.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="recv"><b>recv</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.recv( <i>len</i>, <i>[</i>, <i>flags</i><i>]</i> )
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Receives up to <i>len</i> bytes from <i>aSession</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="send"><b>send</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.send( <i>aString</i>, <i>flags</i>,
<i>[</i>, <i>to</i><i>]</i> ) -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Sends <i>aString</i> over <i>aSession</i>. If specified, <i>to</i> is a
<code>struct sockaddr</code> specifying the recipient
address. <i>flags</i> are the sum or one or more of the
<code>MSG_</code> options (listed on page 482). Returns the
number of characters sent.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="setsockopt"><b>setsockopt</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.setsockopt(
<i>level</i>, <i>optname</i>, <i>optval</i> ) -> 0
</td></tr><td></td><td>
<P></P>
Sets a socket option. <i>level</i> is one of the socket-level
options (listed on page 482). <i>optname</i> and
<i>optval</i> are protocol specific---see your system
documentation for details.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="shutdown"><b>shutdown</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.shutdown( <i>how</i>=2 )
-> 0
</td></tr><td></td><td>
<P></P>
Shuts down the receive (<i>how</i> == 0), or send (<i>how</i> == 1),
or both (<i>how</i> == 2), parts of this socket.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class IPSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">BasicSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#getaddress">getaddress</a> <a href="#addr"><i>addr</i></a> <a href="#peeraddr"><i>peeraddr</i></a> <p></p><hr>
Class <code>IPSocket</code> is a base class for sockets using IP as
their transport. <code>TCPSocket</code> and <code>UDPSocket</code> are based on this
class.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getaddress"><b>getaddress</b></a></font></td><td bgcolor="#ffaaaa">
IPSocket.getaddress( <i>hostName</i> )
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the dotted-quad IP address of <i>hostName</i>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = IPSocket.getaddress('www.ruby-lang.org')</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>"210.251.121.214"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="addr"><b>addr</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.addr -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the domain, port, name, and IP address of <i>aSession</i> as a
four-element array. The name will be returned as an address if
the <code>do_not_reverse_lookup</code> flag is <code>true</code>.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>u = UDPSocket.new</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>u.bind('localhost', 8765)</code></td>
</tr>
<tr>
<td valign="top"><code>u.addr</code></td>
<td valign="top"></td>
<td valign="top"><code>["AF_INET", 8765, "localhost", "127.0.0.1"]</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>BasicSocket.do_not_reverse_lookup = true</code></td>
</tr>
<tr>
<td valign="top"><code>u.addr</code></td>
<td valign="top"></td>
<td valign="top"><code>["AF_INET", 8765, "127.0.0.1", "127.0.0.1"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="peeraddr"><b>peeraddr</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.peeraddr -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the domain, port, name, and IP address of the peer.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class TCPSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">IPSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#gethostbyname">gethostbyname</a> <a href="#new">new</a> <a href="#open">open</a> <a href="#recvfrom"><i>recvfrom</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>t = TCPSocket.new('localhost', 'ftp')</code></td>
</tr>
<tr>
<td valign="top"><code>t.gets</code></td>
<td valign="top"></td>
<td valign="top"><code>"220 zip.local.thomases.com FTP server (Version 6.2/OpenBSD/Linux-0.11) ready.\r\n"</code></td>
</tr>
<tr>
<td valign="top"><code>t.close</code></td>
<td valign="top"></td>
<td valign="top"><code>nil</code></td>
</tr>
</table>
<P></P>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gethostbyname"><b>gethostbyname</b></a></font></td><td bgcolor="#ffaaaa">
TCPSocket.gethostbyname(
<i>hostName</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Looks up <i>hostName</i> and returns its canonical name, an array
containing any aliases, the address
type (<code>AF_INET</code>), and the dotted-quad IP address.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = TCPSocket.gethostbyname('ns.pragprog.com')</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>["pragprog.com", [], 2, "63.68.129.131"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
TCPSocket.new( <i>hostName</i>, <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Opens a TCP connection to <i>hostName</i> on the <i>port</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
TCPSocket.open( <i>hostName</i>, <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>TCPSocket#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="recvfrom"><b>recvfrom</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.recvfrom(
<i>len</i> <i>[</i>, <i>flags</i><i>]</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Receives up to <i>len</i> bytes on the connection. <i>flags</i> is
zero or more of the <code>MSG_</code> options (listed
on page 482).
Returns a two-element
array. The first element is the received data, the second is an
array containing information about the peer.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>t = TCPSocket.new('localhost', 'ftp')</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>data = t.recvfrom(30)</code></td>
</tr>
<tr>
<td valign="top"><code>data</code></td>
<td valign="top"></td>
<td valign="top"><code>["220 zip.local.thomases.com FTP", ["AF_INET", 21, "localhost", "127.0.0.1"]]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class SOCKSSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">TCPSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#close"><i>close</i></a> <p></p><hr>
<P></P>
Class <code>SOCKSSocket</code> supports connections based on the SOCKS protocol.
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
SOCKSSocket.new( <i>hostName</i>, <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Opens a SOCKS connection to <i>port</i> on <i>hostName</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
SOCKSSocket.open( <i>hostName</i>, <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>SOCKSSocket#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="close"><b>close</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.close -> <code>nil</code>
</td></tr><td></td><td>
<P></P>
Closes this SOCKS connection.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class TCPServer</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">TCPSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#accept"><i>accept</i></a> <p></p><hr>
A <code>TCPServer</code> accepts incoming TCP connections. Here is a Web
server that listens on a given port and returns the time.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'socket'
port = (ARGV[0] || 80).to_i
server = TCPServer.new('localhost', port)
while (session = server.accept)
puts "Request: #{session.gets}"
session.print "HTTP/1.1 200/OK\r\nContent-type: text/html\r\n\r\n"
session.print "<html><body><h1>#{Time.now}</h1></body></html>\r\n"
session.close
end
</pre></td></tr></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
TCPServer.new( <i>[</i><i>hostName</i>,<i>]</i> <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
Creates a new socket on the given
interface (identified by <i>hostName</i> and port). If
<i>hostName</i> is omitted, the server will listen on all
interfaces on the current host (equivalent to an address of
0.0.0.0).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
TCPServer.open( <i>[</i><i>hostName</i>,<i>]</i> <i>port</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>TCPServer#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="accept"><b>accept</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.accept -> <i>aTCPSocket</i>
</td></tr><td></td><td>
<P></P>
Waits for a connection on <i>aSession</i>, and returns a new <code>TCPSocket</code>
connected to the caller. See the example
on page 478.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class UDPSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">IPSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#bind"><i>bind</i></a> <a href="#connect"><i>connect</i></a> <a href="#recvfrom"><i>recvfrom</i></a> <a href="#send"><i>send</i></a> <p></p><hr>
<P></P>
UDP sockets send and receive datagrams. In order to receive data, a
socket must be bound to a particular port. You have two choices when
sending data: you can connect to a remote UDP socket and thereafter
send datagrams to that port, or you can specify a host and port for
use with every packet you send. This example is a UDP server that
prints the message it receives. It is called by both connectionless and
connection-based clients.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'socket'
<P></P>
$port = 4321
<P></P>
sThread = Thread.start do # run server in a thread
server = UDPSocket.open
server.bind(nil, $port)
2.times { p server.recvfrom(64) }
end
<P></P>
# Ad-hoc client
UDPSocket.open.send("ad hoc", 0, 'localhost', $port)
<P></P>
# Connection based client
sock = UDPSocket.open
sock.connect('localhost', $port)
sock.send("connection-based", 0)
sThread.join
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
["ad hoc", ["AF_INET", 1544, "localhost", "127.0.0.1"]]
["connection-based", ["AF_INET", 1545, "localhost", "127.0.0.1"]]
</pre></td></tr></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
UDPSocket.new( <i>family</i> = <code>AF_INET</code> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates an endpoint for UDP communications, optionally specifying
the address family.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
UDPSocket.open( <i>family</i> = <code>AF_INET</code>
)
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>UDPSocket#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="bind"><b>bind</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.bind( <i>hostName</i>, <i>port</i> )
-> 0
</td></tr><td></td><td>
<P></P>
Associates the local end of the UDP connection with a given
<i>hostName</i> and <i>port</i>. Must be used by servers to
establish an accessible endpoint.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="connect"><b>connect</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.connect( <i>hostName</i>, <i>port</i> )
-> 0
</td></tr><td></td><td>
<P></P>
Creates a connection to the given <i>hostName</i> and <i>port</i>.
Subsequent
<code>UDPSocket#send</code> requests that don't override the recipient
will use this connection. Multiple <code>connect</code> requests may be
issued on <i>aSession</i>: the most recent will be used by <code>send</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="recvfrom"><b>recvfrom</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.recvfrom(
<i>len</i> <i>[</i>, <i>flags</i><i>]</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Receives up to <i>len</i> bytes from <i>aSession</i>. <i>flags</i> is zero
or more of the <code>MSG_</code> options (listed
on page 482).
The result is a two-element
array containing the received data and information on the
sender. See the example on page 479.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="send"><b>send</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.send( <i>aString</i>, <i>flags</i> )
-> <i>aFixnum</i><br><i>aSession</i>.send( <i>aString</i>, <i>flags</i>, <i>hostName</i>, <i>port</i> )
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
The two-parameter form sends <i>aString</i> on an existing
connection. The four-parameter form sends <i>aString</i> to
<i>port</i> on <i>hostName</i>.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class UNIXSocket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">BasicSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#addr"><i>addr</i></a> <a href="#path"><i>path</i></a> <a href="#peeraddr"><i>peeraddr</i></a> <a href="#recvfrom"><i>recvfrom</i></a> <p></p><hr>
Class <code>UNIXSocket</code> supports interprocess communications using the Unix
domain protocol. Although the underlying protocol supports both
datagram and stream connections, the Ruby library provides only a
stream-based connection.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'socket'
<P></P>
$path = "/tmp/sample"
<P></P>
sThread = Thread.start do # run server in a thread
sock = UNIXServer.open($path)
s1 = sock.accept
p s1.recvfrom(124)
end
<P></P>
client = UNIXSocket.open($path)
client.send("hello", 0)
client.close
<P></P>
sThread.join
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
["hello", ["AF_UNIX", ""]]
</pre></td></tr></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
UNIXSocket.new( <i>path</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Opens a new domain socket on <i>path</i>, which must be a pathname.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
UNIXSocket.open( <i>path</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>UNIXSocket#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="addr"><b>addr</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.addr -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the address family and path of this socket.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="path"><b>path</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.path -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the path of this domain socket.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="peeraddr"><b>peeraddr</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.peeraddr -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the address family and path of the server end of the connection.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="recvfrom"><b>recvfrom</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.recvfrom(
<i>len</i> <i>[</i>, <i>flags</i><i>]</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Receives up to <i>len</i> bytes from <i>aSession</i>. <i>flags</i> is
zero or more of the <code>MSG_</code> options (listed
on page 482).
The first element of the
returned array is the received data, and the second contains
(minimal) information on the sender.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class UNIXServer</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">UNIXSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#accept"><i>accept</i></a> <p></p><hr>
<P></P>
Class <code>UNIXServer</code> provides a simple Unix domain socket
server. See <code>UNIXSocket</code> for example code.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
UNIXServer.new( <i>path</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates a server on the given <i>path</i>. The corresponding file
must not exist at the time of the call.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
UNIXServer.open( <i>path</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>UNIXServer#new</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="accept"><b>accept</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.accept
-> <i>aUnixSocket</i>
</td></tr><td></td><td>
<P></P>
Waits for a connection on the server socket and returns a new
socket object for that connection. See the example for
<code>UNIXSocket</code> on page 480.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Socket</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">BasicSocket</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#for_fd">for_fd</a> <a href="#getaddrinfo">getaddrinfo</a> <a href="#gethostbyaddr">gethostbyaddr</a> <a href="#gethostbyname">gethostbyname</a> <a href="#gethostname">gethostname</a> <a href="#getnameinfo">getnameinfo</a> <a href="#getservbyname">getservbyname</a> <a href="#new">new</a> <a href="#open">open</a> <a href="#pair">pair</a> <a href="#socketpair">socketpair</a> <a href="#accept"><i>accept</i></a> <a href="#bind"><i>bind</i></a> <a href="#connect"><i>connect</i></a> <a href="#listen"><i>listen</i></a> <a href="#recvfrom"><i>recvfrom</i></a> <p></p><hr>
<P></P>
Class <code>Socket</code> provides access to the underlying operating system
socket implementation. It can be used to provide more
operating system-specific
functionality than the protocol-specific socket classes, but at the
expense of greater complexity. In particular, the class handles
addresses using <code>struct sockaddr</code> structures packed into Ruby
strings, which can be a joy to manipulate.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="3" bgcolor="990066"><font color="white" size="6">
constants
</font></td></tr></table><p></p>
Class <code>Socket</code> defines constants for use throughout the socket
library. Individual constants are available only on architectures
that support the related facility.
<P></P>
<dl><dt>Types:</dt><dd><br><code>SOCK_DGRAM</code>,
<code>SOCK_PACKET</code>,
<code>SOCK_RAW</code>,
<code>SOCK_RDM</code>,
<code>SOCK_SEQPACKET</code>,
<code>SOCK_STREAM</code>.
<P></P>
</dd><dt>Protocol families:</dt><dd><br><code>PF_APPLETALK</code>,
<code>PF_AX25</code>,
<code>PF_INET6</code>,
<code>PF_INET</code>,
<code>PF_IPX</code>,
<code>PF_UNIX</code>,
<code>PF_UNSPEC</code>.
<P></P>
</dd><dt>Address families:</dt><dd><br><code>AF_APPLETALK</code>,
<code>AF_AX25</code>,
<code>AF_INET6</code>,
<code>AF_INET</code>,
<code>AF_IPX</code>,
<code>AF_UNIX</code>,
<code>AF_UNSPEC</code>.
<P></P>
</dd><dt>Lookup-order options:</dt><dd><br><code>LOOKUP_INET6</code>,
<code>LOOKUP_INET</code>,
<code>LOOKUP_UNSPEC</code>.
<P></P>
</dd><dt>Send/receive options:</dt><dd><br><code>MSG_DONTROUTE</code>,
<code>MSG_OOB</code>,
<code>MSG_PEEK</code>.
<P></P>
</dd><dt>Socket-level options:</dt><dd><br><code>SOL_ATALK</code>,
<code>SOL_AX25</code>,
<code>SOL_IPX</code>,
<code>SOL_IP</code>,
<code>SOL_SOCKET</code>,
<code>SOL_TCP</code>,
<code>SOL_UDP</code>.
<P></P>
</dd><dt>Socket options:</dt><dd><br><code>SO_BROADCAST</code>,
<code>SO_DEBUG</code>,
<code>SO_DONTROUTE</code>,
<code>SO_ERROR</code>,
<code>SO_KEEPALIVE</code>,
<code>SO_LINGER</code>,
<code>SO_NO_CHECK</code>,
<code>SO_OOBINLINE</code>,
<code>SO_PRIORITY</code>,
<code>SO_RCVBUF</code>,
<code>SO_REUSEADDR</code>,
<code>SO_SNDBUF</code>,
<code>SO_TYPE</code>.
<P></P>
</dd><dt>QOS options:</dt><dd><br><code>SOPRI_BACKGROUND</code>,
<code>SOPRI_INTERACTIVE</code>,
<code>SOPRI_NORMAL</code>.
<P></P>
</dd><dt>Multicast options:</dt><dd><br><code>IP_ADD_MEMBERSHIP</code>,
<code>IP_DEFAULT_MULTICAST_LOOP</code>,
<code>IP_DEFAULT_MULTICAST_TTL</code>,
<code>IP_MAX_MEMBERSHIPS</code>,
<code>IP_MULTICAST_IF</code>,
<code>IP_MULTICAST_LOOP</code>,
<code>IP_MULTICAST_TTL</code>.
<P></P>
</dd><dt>TCP options:</dt><dd><br><code>TCP_MAXSEG</code>,
<code>TCP_NODELAY</code>.
<P></P>
</dd><dt><code>getaddrinfo</code> error codes:</dt><dd><br><code>EAI_ADDRFAMILY</code>,
<code>EAI_AGAIN</code>,
<code>EAI_BADFLAGS</code>,
<code>EAI_BADHINTS</code>,
<code>EAI_FAIL</code>,
<code>EAI_FAMILY</code>,
<code>EAI_MAX</code>,
<code>EAI_MEMORY</code>,
<code>EAI_NODATA</code>,
<code>EAI_NONAME</code>,
<code>EAI_PROTOCOL</code>,
<code>EAI_SERVICE</code>,
<code>EAI_SOCKTYPE</code>,
<code>EAI_SYSTEM</code>.
<P></P>
</dd><dt><code>ai_flags</code> values:</dt><dd><br><code>AI_ALL</code>,
<code>AI_CANONNAME</code>,
<code>AI_MASK</code>,
<code>AI_NUMERICHOST</code>,
<code>AI_PASSIVE</code>,
<code>AI_V4MAPPED_CFG</code>.
</dd></dl>
<p></p>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="for_fd"><b>for_fd</b></a></font></td><td bgcolor="#ffaaaa">
Socket.for_fd( <i>anFD</i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Wraps an already open file descriptor into a socket object.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getaddrinfo"><b>getaddrinfo</b></a></font></td><td bgcolor="#ffaaaa">
Socket.getaddrinfo(
<i>hostName</i>, <i>port</i>,<br><i>[</i><i>family</i>
<i>[</i><i>socktype</i> <i>[</i><i>protocol</i> <i>[</i><i>flags</i><i>]</i><i>]</i><i>]</i><i>]</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of arrays describing the given host and
port (optionally qualified as shown). Each subarray
contains the address family, port number, host name, host IP
address, protocol family, socket type, and protocol.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
for line in Socket.getaddrinfo('www.microsoft.com', 'http')
puts line.join(", ")
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
AF_INET, 80, microsoft.net, 207.46.130.149, 2, 1, 6
AF_INET, 80, microsoft.net, 207.46.131.137, 2, 1, 6
AF_INET, 80, microsoft.com, 207.46.230.218, 2, 1, 6
AF_INET, 80, microsoft.com, 207.46.230.219, 2, 1, 6
AF_INET, 80, microsoft.net, 207.46.130.14, 2, 1, 6
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gethostbyaddr"><b>gethostbyaddr</b></a></font></td><td bgcolor="#ffaaaa">
Socket.gethostbyaddr(
<i>addr</i>, <i>type</i>=<code>AF_INET</code> )
-> <i>anArray</i>
</td></tr><td></td><td>
Returns the host name, address family, and <code>sockaddr</code> component
for the given address.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Socket.gethostbyname("63.68.129.130")</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>res = Socket.gethostbyaddr(a[3], a[2])</code></td>
</tr>
<tr>
<td valign="top"><code>res.join(', ')</code></td>
<td valign="top"></td>
<td valign="top"><code>"somewhere.in.pragprog.com, , 2, ?D\201\202"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gethostbyname"><b>gethostbyname</b></a></font></td><td bgcolor="#ffaaaa">
Socket.gethostbyname( <i>hostName</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a four-element array containing the canonical host name,
a subarray of host aliases, the address family, and the address
portion of the <code>sockaddr</code> structure.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Socket.gethostbyname("63.68.129.130")</code></td>
</tr>
<tr>
<td valign="top"><code>a.join(', ')</code></td>
<td valign="top"></td>
<td valign="top"><code>"somewhere.in.pragprog.com, , 2, ?D\201\202"</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gethostname"><b>gethostname</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.gethostname
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the name of the current host.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getnameinfo"><b>getnameinfo</b></a></font></td><td bgcolor="#ffaaaa">
Socket.getnameinfo(
<i>addr</i> <i>[</i>, <i>flags</i><i>]</i> )
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Looks up the given address, which may be either a string
containing a sockaddr or a three- or four-element array. If
<i>sockaddr</i> is an array, it should contain the string address
family, the port (or nil), and the host name or IP address. If a
fourth element is present and not <code>nil</code>, it will be used as the host name.
Returns a canonical hostname (or address) and port number as an array.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>a = Socket.getnameinfo(["AF_INET", '23', 'www.ruby-lang.org'])</code></td>
</tr>
<tr>
<td valign="top"><code>a</code></td>
<td valign="top"></td>
<td valign="top"><code>["helium.ruby-lang.org", "telnet"]</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getservbyname"><b>getservbyname</b></a></font></td><td bgcolor="#ffaaaa">
Socket.getservbyname(
<i>service</i>, <i>proto</i>=<code>'tcp'</code> )
-> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the port corresponding to the given service and
protocol.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td valign="top"><code>Socket.getservbyname("telnet")</code></td>
<td valign="top"></td>
<td valign="top"><code>23</code></td>
</tr>
</table>
<P></P>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Socket.new( <i>domain</i>, <i>type</i>,
<i>protocol</i> ) -> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates a socket using the given parameters.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
Socket.open( <i>domain</i>, <i>type</i>,
<i>protocol</i> ) -> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>Socket#new</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="pair"><b>pair</b></a></font></td><td bgcolor="#ffaaaa">
Socket.pair( <i>domain</i>, <i>type</i>,
<i>protocol</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns a pair of connected, anonymous sockets of the given
domain, type, and protocol.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="socketpair"><b>socketpair</b></a></font></td><td bgcolor="#ffaaaa">
Socket.socketpair( <i>domain</i>, <i>type</i>,
<i>protocol</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Synonym for <code>Socket#pair</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="accept"><b>accept</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.accept -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Accepts an incoming connection returning an array containing a
new <code>Socket</code> object and a string holding the <code>struct
sockaddr</code> information about the caller.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="bind"><b>bind</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.bind( <i>sockaddr</i> )
-> 0
</td></tr><td></td><td>
<P></P>
Binds to the given <code>struct sockaddr</code>, contained in a string.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="connect"><b>connect</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.connect( <i>sockaddr</i> )
-> 0
</td></tr><td></td><td>
<P></P>
Connects to the given <code>struct sockaddr</code>, contained in a string.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="listen"><b>listen</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.listen( <i>aFixnum</i> )
-> 0
</td></tr><td></td><td>
<P></P>
Listens for connections, using the specified <i>aFixnum</i> as
the backlog.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="recvfrom"><b>recvfrom</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.recvfrom(
<i>len</i> <i>[</i>, <i>flags</i><i>]</i> ) -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Receives up to <i>len</i> bytes from <i>aSession</i>. <i>flags</i> is
zero or more of the <code>MSG_</code> options. The first element of the
result is the data received. The second element contains
protocol-specific information on the sender.
<P></P>
</td></table>
<P></P>
<P></P>
<h2>Higher-Level Access</h2>
<P></P>
Ruby provides a set of classes to facilitate writing clients for:
<ul>
<li> File Transfer Protocol (FTP)
</li><li> HyperText Transfer Protocol (HTTP)
</li><li> Post Office Protocol (POP)
</li><li> Simple Mail Transfer Protocol (SMTP)
</li><li> Telnet
</li></ul>
<P></P>
HTTP, POP, and SMTP are layered on top of a helper class,
<code>lib/net/protocol</code>. Although we don't document the <code>Protocol</code>
class here, you should probably study it if you are considering
writing your own network client.
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::FTP</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#open">open</a> <a href="#Servercommands"><i>Server commands</i></a> <a href="#close"><i>close</i></a> <a href="#closed_qm"><i>closed?</i></a> <a href="#connect"><i>connect</i></a> <a href="#debug_mode"><i>debug_mode</i></a> <a href="#debug_mode_eq"><i>debug_mode=</i></a> <a href="#dir"><i>dir</i></a> <a href="#getbinaryfile"><i>getbinaryfile</i></a> <a href="#gettextfile"><i>gettextfile</i></a> <a href="#lastresp"><i>lastresp</i></a> <a href="#list"><i>list</i></a> <a href="#login"><i>login</i></a> <a href="#ls"><i>ls</i></a> <a href="#mtime"><i>mtime</i></a> <a href="#passive"><i>passive</i></a> <a href="#passive_eq"><i>passive=</i></a> <a href="#putbinaryfile"><i>putbinaryfile</i></a> <a href="#puttextfile"><i>puttextfile</i></a> <a href="#resume"><i>resume</i></a> <a href="#resume_eq"><i>resume=</i></a> <a href="#retrbinary"><i>retrbinary</i></a> <a href="#retrlines"><i>retrlines</i></a> <a href="#return_code"><i>return_code</i></a> <a href="#storbinary"><i>storbinary</i></a> <a href="#storlines"><i>storlines</i></a> <a href="#welcome"><i>welcome</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/ftp'
<P></P>
ftp = Net::FTP.new('ftp.netlab.co.jp')
ftp.login
files = ftp.chdir('pub/lang/ruby/contrib')
files = ftp.list('n*')
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
ftp.close
</pre></td></tr></table>
<P></P>
The <code>net/ftp</code> library implements a File Transfer Protocol (FTP)
client.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="3" bgcolor="990066"><font color="white" size="6">
constants
</font></td></tr></table><table border="2" cellpadding="5">
<tr>
<td valign="top"><tr>
<td><a name="FTP_PORT"><code>FTP_PORT</code></a></td>
<td></td>
<td>Default port for FTP connections (21).</td>
</tr>
</td>
</tr>
</table><p></p>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
FTP.new( <i>host</i>=<code>nil</code>,
<i>user</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>,
<i>acct</i>=<code>nil</code>) -> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates and returns a new <code>FTP</code> object. If the host parameter is
not <code>nil</code>, a connection is made to that
host. Additionally, if the <i>user</i> parameter is not <code>nil</code>, the
given user name, password, and (optionally) account are
used to log in. See the description of <code>FTP#login</code>
on page 488.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="open"><b>open</b></a></font></td><td bgcolor="#ffaaaa">
FTP.open( <i>host</i>,
<i>user</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>,
<i>acct</i>=<code>nil</code>) -> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
A synonym for <code>FTP#new</code>, but with a mandatory <i>host</i>
parameter.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="Servercommands"><b>Server commands</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.acct( <i>account</i> )<br><i>aSession</i>.chdir( <i>dir</i> )<br><i>aSession</i>.delete( <i>remoteFile</i> )<br><i>aSession</i>.mdtm( <i>remoteFile</i> ) -> <i>aString</i><br><i>aSession</i>.mkdir( <i>dir</i> )<br><i>aSession</i>.nlst( <i>dir</i>=<code>nil</code> ) -> <i>anArray</i><br><i>aSession</i>.rename( <i>fromname</i>, <i>toname</i> )<br><i>aSession</i>.rmdir( <i>dir</i> )<br><i>aSession</i>.pwd -> <i>aString</i><br><i>aSession</i>.size( <i>remoteFile</i> ) -> <i>anInteger</i><br><i>aSession</i>.status -> <i>aString</i><br><i>aSession</i>.system -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Issues the corresponding server command and returns the result.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="close"><b>close</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.close
</td></tr><td></td><td>
<P></P>
Closes the current connection.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="closed_qm"><b>closed?</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.closed? -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the current connection is closed.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="connect"><b>connect</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.connect( <i>host</i>,
<i>port</i>=FTP_PORT )
</td></tr><td></td><td>
<P></P>
Establishes an FTP connection to <i>host</i>, optionally overriding
the default port. If the environment variable
<code>SOCKS_SERVER</code>
is set, sets up the connection through
a SOCKS proxy. Raises an exception (typically
<code>Errno::ECONNREFUSED</code>) if the connection
cannot be established.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="debug_mode"><b>debug_mode</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.debug_mode -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the current debug mode.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="debug_mode_eq"><b>debug_mode=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.debug_mode = <code>true</code> or <code>false</code></td></tr><td></td><td>
<P></P>
If the debug mode is <code>true</code>, all traffic to and from the
server is written to <code>$stdout</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="dir"><b>dir</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.dir( <i>[</i>pattern<i>]<sup>*</sup></i> ) -> <i>anArray</i><br><i>aSession</i>.dir( <i>[</i>pattern<i>]<sup>*</sup></i> ) {| line | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Synonym for <code>FTP#list</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="getbinaryfile"><b>getbinaryfile</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.getbinaryfile(
<i>remotefile</i>, <i>localfile</i>, <i>blocksize</i>,
<i>callback</i>=<code>nil</code>)<br><i>aSession</i>.getbinaryfile(
<i>remotefile</i>, <i>localfile</i>, <i>blocksize</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Retrieves <i>remotefile</i> in binary mode, storing the result in
<i>localfile</i>. If <i>callback</i> or an associated block is
supplied, calls it, passing in the retrieved data in
<i>blocksize</i> chunks.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="gettextfile"><b>gettextfile</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.gettextfile(
<i>remotefile</i>, <i>localfile</i>,
<i>callback</i>=<code>nil</code>)<br><i>aSession</i>.gettextfile(
<i>remotefile</i>, <i>localfile</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Retrieves <i>remotefile</i> in ASCII (text) mode, storing the result in
<i>localfile</i>. If <i>callback</i> or an associated block is
supplied, calls it, passing in the retrieved data one line at a time.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="lastresp"><b>lastresp</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.lastresp -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the host's last response.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="list"><b>list</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.list( <i>[</i>pattern<i>]<sup>*</sup></i> ) -> <i>anArray</i><br><i>aSession</i>.list( <i>[</i>pattern<i>]<sup>*</sup></i> ) {| line | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Fetches a directory listing of files matching the given
pattern(s). If a block is associated with the call, invokes it
with each line of the result. Otherwise, returns the result as an
array of strings.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="login"><b>login</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.login( <i>user</i>="anonymous",
<i>passwd</i>=<code>nil</code>, <i>acct</i>=<code>nil</code> ) -> aString
</td></tr><td></td><td>
<P></P>
Logs into
the remote host. <i>aSession</i> must have been previously
connected. If <i>user</i> is the string ``anonymous'' and the
password is <code>nil</code>, a password of <em>user@host</em> is
synthesized. If the <i>acct</i> parameter is not <code>nil</code>, an FTP
<code>ACCT</code> command is sent following the successful
login. Raises an exception on error (typically
<code>Net::FTPPermError</code>).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="ls"><b>ls</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.ls( <i>[</i>pattern<i>]<sup>*</sup></i> ) -> <i>anArray</i><br><i>aSession</i>.ls( <i>[</i>pattern<i>]<sup>*</sup></i> ) {| line | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Synonym for <code>FTP#list</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="mtime"><b>mtime</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.mtime( <i>remoteFile</i>,
<i>local</i>=<code>false</code> ) -> <i>aTime</i>
</td></tr><td></td><td>
<P></P>
Returns the last-modified time of <i>remoteFile</i>, interpreting
the server's response as a GMT time if <i>local</i> is
<code>false</code>, or as a local time otherwise.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="passive"><b>passive</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.passive -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the state of the <code>passive</code> flag.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="passive_eq"><b>passive=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.passive = <code>true</code> or <code>false</code></td></tr><td></td><td>
<P></P>
Puts the connection into passive mode if <code>true</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="putbinaryfile"><b>putbinaryfile</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.putbinaryfile(
<i>localfile</i>, <i>remotefile</i>, <i>blocksize</i>,
<i>callback</i>=<code>nil</code>)<br><i>aSession</i>.putbinaryfile(
<i>localfile</i>, <i>remotefile</i>, <i>blocksize</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Transfers <i>localfile</i> to the server in binary mode, storing
the result in
<i>remotefile</i>. If <i>callback</i> or an associated block is
supplied, calls it, passing in the transmitted data in
<i>blocksize</i> chunks.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="puttextfile"><b>puttextfile</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.puttextfile(
<i>localfile</i>, <i>remotefile</i>,
<i>callback</i>=<code>nil</code>)<br><i>aSession</i>.puttextfile(
<i>localfile</i>, <i>remotefile</i>, <i>blocksize</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Transfers <i>localfile</i> to the server in ASCII (text) mode,
storing the result in <i>remotefile</i>. If <i>callback</i> or an
associated block is supplied, calls it, passing in the
transmitted data one line at a time.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="resume"><b>resume</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.resume -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the status of the <em>resume</em> flag (see
<code>FTP#resume=</code>). Default is <code>false</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="resume_eq"><b>resume=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.resume=<i>aBoolean</i>
</td></tr><td></td><td>
<P></P>
Sets the status of the <em>resume</em> flag. When <em>resume</em> is
<code>true</code>, partially received files will resume where they
left off, instead of starting from the beginning again. This is
done by sending a <code>REST</code> command (<code>REST</code>art incomplete
transfer) to the server.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="retrbinary"><b>retrbinary</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.retrbinary(
<i>cmd</i>, <i>blocksize</i> ) {| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Puts the connection into binary (image) mode, issues the given
command, and fetches the data returned, passing it to the associated
block in chunks of <i>blocksize</i> characters. Note that
<i>cmd</i> is a server command (such as ``RETR myfile'').
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="retrlines"><b>retrlines</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.retrlines(cmd) {| line | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Puts the connection into ASCII (text) mode, issues the given
command, and passes the resulting data, one line at a time, to the
associated block. If no block is given, prints the lines. Note that
<i>cmd</i> is a server command (such as ``RETR myfile'').
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="return_code"><b>return_code</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.return_code -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the return code from the last operation.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="storbinary"><b>storbinary</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.storbinary( <i>cmd</i>, <i>fileName</i>, <i>blocksize</i>,
<i>callback</i>=<code>nil</code>) <br><i>aSession</i>.storbinary( <i>cmd</i>, <i>fileName</i>, <i>blocksize</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Puts the connection into binary (image) mode, issues the given
server-side command (such as ``STOR myfile''), and sends the
contents of the file named <i>fileName</i> to the server. If the optional
block is given, or if the <i>callBack</i> parameter is a <code>Proc</code>,
also passes it the data, in chunks of <i>blocksize</i> characters.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="storlines"><b>storlines</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.storlines( <i>cmd</i>, <i>fileName</i>,
<i>callback</i>=<code>nil</code>) <br><i>aSession</i>.storlines( <i>cmd</i>, <i>fileName</i> )
{| data | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Puts the connection into ASCII (text) mode, issues the given
server-side command (such as ``STOR myfile''), and sends the
contents of the file named <i>fileName</i> to the server, one line at a
time. If the optional
block is given, or if the <i>callBack</i> parameter is a <code>Proc</code>,
also passes it the lines.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="welcome"><b>welcome</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.welcome -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the host's welcome message.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::HTTP</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Net::Protocol</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#port">port</a> <a href="#start">start</a> <a href="#get"><i>get</i></a> <a href="#head"><i>head</i></a> <a href="#post"><i>post</i></a> <a href="#start"><i>start</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/http'
<P></P>
h = Net::HTTP.new('www.pragmaticprogrammer.com', 80)
resp, data = h.get('/index.html', nil )
puts "Code = #{resp.code}"
puts "Message = #{resp.message}"
resp.each {|key, val| printf "%-14s = %-40.40s\n", key, val }
p data[0..55]
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Code = 200
Message = OK
content-type = text/html
last-modified = Wed, 21 Feb 2001 18:52:26 GMT
date = Mon, 05 Mar 2001 05:26:29 GMT
connection = close
accept-ranges = bytes
etag = "804d98-1a4b-3a940e6a"
content-length = 6731
server = Rapidsite/Apa-1.3.4 FrontPage/4.0.4.3
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional"
</pre></td></tr></table>
<P></P>
The <code>net/http</code> library provides a simple client to fetch headers
and Web page contents using the HTTP protocol.
<P></P>
The <code>get</code>, <code>post</code>, and <code>head</code> requests raise
exceptions on any error, including some HTTP status responses that
would normally be considered recoverable. There are two ways of
handling these.
<P></P>
<ol>
<li> Each method has a corresponding version <code>get2</code>,
<code>post2</code>, or <code>head2</code> that does not raise an
exception. These versions are documented in the source.
</li><li> Recoverable errors raise a
<code>Net::ProtoRetriableError</code> exception. This exception contains a
<code>data</code> attribute containing the response returned by the
server.
</li></ol>
<P></P>
The code below illustrates the handling of an HTTP status 301, a redirect.
It uses Tomoyuki Kosimizu's URI
package, available in the RAA.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
h = Net::HTTP.new(ARGV[0] || 'www.ruby-lang.org', 80)
url = ARGV[1] || '/'
<P></P>
begin
resp, data = h.get(url, nil) { |a| }
rescue Net::ProtoRetriableError => detail
head = detail.data
<P></P>
if head.code == "301"
uri = URI.create(head['location'])
<P></P>
host = uri['host']
url = uri['path']
port = uri['port']
<P></P>
h.finish
h = Net::HTTP.new(host, port)
<P></P>
retry
end
end
</pre></td></tr></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Net::HTTP.new( <i>host</i>='localhost',
<i>port</i>=80,
<i>proxy</i>=<code>nil</code>, <i>proxy_port</i>=<code>nil</code> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates and returns a new <code>HTTP</code> object. No connection is
made until <code>HTTP#start</code> is called.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="port"><b>port</b></a></font></td><td bgcolor="#ffaaaa">
Net::HTTP.port -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the default HTTP port (80).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
Net::HTTP.start( <i>host</i>=<code>nil</code>, <i>port</i>=80 )<br>Net::HTTP.start( <i>host</i>=<code>nil</code>, <i>port</i>=80 )
{| <i>aSession</i> | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>Net::HTTP.new(<i>host</i>, <i>port</i>).start</code>.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="get"><b>get</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.get( <i>path</i>, <i>headers</i>=<code>nil</code>, <i>dest</i>="" )
-> <i>anArray</i><br><i>aSession</i>.get( <i>path</i>, <i>headers</i>=<code>nil</code>) {| result | block }
<P></P>
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Retrieves headers and content from the specified <i>path</i> on
the host specified when <i>aSession</i> was created. If specified, the
<i>headers</i> parameter is a <code>Hash</code> containing additional
header names and values to be sent with the request. The method
returns a two-element array. The first element is an
<code>HTTPResponse</code> object (documented in the next section).
The second element is the page's content.
The page's content is also passed to the <code><<</code> method of
the <i>dest</i> parameter, or to the block if specified. This
result is built network block by network block, not line by line.
An exception is raised
if an error is encountered. Multiple <code>get</code> calls may be
made on <i>aSession</i>. Unless <code>Protocol#finish</code> is explicitly
called, the connection will use the HTTP/1.1 keep-alive
protocol, and will not close between requests.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="head"><b>head</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.head( <i>path</i>, <i>headers</i>=<code>nil</code> )
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Retrieves headers from the specified <i>path</i> on the host
specified when <i>aSession</i> was created. If specified, the
<i>headers</i> parameter is a hash containing additional
header names and values to be sent with the request. The method
returns a hash of received headers. An exception is raised if
an error is encountered. Multiple <code>head</code> calls may be
made on <i>aSession</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="post"><b>post</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.post( <i>path</i>, <i>data</i>, <i>headers</i>=<code>nil</code>,
<i>dest</i>="" ) -> <i>anArray</i><br><i>aSession</i>.post( <i>path</i>, <i>data</i>, <i>headers</i>=<code>nil</code> )
{| result | block }
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Sends <i>data</i> to <i>path</i> using an HTTP POST
request. <i>headers</i> is a hash containing additional
headers. Assigns the result to <i>data</i> or to the block, as
for <code>Net_HTTP#get</code>. Returns a two-element array containing
an HTTPResponse object and the reply body.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.start<br><i>aSession</i>.start {| <i>aSession</i> | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Establishes a connection to the host associated with
<i>aSession</i>. (<code>start</code> is actually a method in
<code>Net::Protocol</code>, but its use is required in HTTP objects.) In
the block form, closes the session at the end of the block.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::HTTPResponse</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white"></font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#_ob_cb"><i>[ ]</i></a> <a href="#_ob_cb_eq"><i>[ ]=</i></a> <a href="#code"><i>code</i></a> <a href="#each"><i>each</i></a> <a href="#key_qm"><i>key?</i></a> <a href="#message"><i>message</i></a> <p></p><hr>
Represents an HTTP response to a GET or POST request.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>[ <i>aKey</i> ]
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the header corresponding to the case-insensitive
key. For example, a key of ``Content-type'' might return
``text/html''.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb_eq"><b>[ ]=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>[ <i>aKey</i> ] = <i>aString</i>
</td></tr><td></td><td>
<P></P>
Sets the header corresponding to the case-insensitive
key.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="code"><b>code</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.code -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the result code from the request (for example, ``404'').
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each"><b>each</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.each {| key, val | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Iterates over all the header key-value pairs.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="key_qm"><b>key?</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.key?( <i>aKey</i> ) -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> only if a header with the given key exists.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="message"><b>message</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.message -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the result message from the request (for example, ``Not found'').
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::POP</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Net::Protocol</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#each"><i>each</i></a> <a href="#finish"><i>finish</i></a> <a href="#mails"><i>mails</i></a> <a href="#start"><i>start</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/pop'
pop = Net::POP3.new('server.ruby-stuff.com')
pop.start('user', 'secret') do |pop|
msg = pop.mails[0]
<P></P>
# Print the 'From:' header line
puts msg.header.split("\r\n").grep(/^From: /)
<P></P>
# Put message to $stdout (by calling <<)
puts "\nFull message:\n"
msg.all($stdout)
end
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
From: Dave Thomas <dave@pragmaticprogrammer.com>
<P></P>
Full message:
Return-Path: <dave@pragmaticprogrammer.com>
Received: (from dave@pragmaticprogrammer.com)
by pragmaticprogrammer.com (8.8.7/8.8.7) id XAA10537;
Sun, 21 May 2000 23:45:58 -0500
Date: Sun, 21 May 2000 23:45:58 -0500
From: Dave Thomas <dave@pragmaticprogrammer.com>
Message-Id: <200005220445.XAA10537@pragmaticprogrammer.com>
To: user@ruby-stuff.com
Subject: New Ruby Version
Status: RO
<P></P>
Just to let you know there's a new Ruby version out.
</pre></td></tr></table>
<P></P>
The <code>net/pop</code> library provides a simple client to fetch and
delete mail on a Post Office Protocol (POP) server.
<P></P>
The class <code>Net::POP3</code> is used to access a POP server, returning
a list of <code>Net::POPMail</code> objects, one per message stored on the
server. These <code>POPMail</code> objects are then used to fetch and/or
delete individual messages.
The library also provides an alternative to the <code>POP3</code> class that
performs <code>APOP</code> authentication.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
HTTP.new( <i>host</i>='localhost', <i>port</i>=110 )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Creates and returns a new <code>POP3</code> object. No connection is
made until <code>POP3#start</code> is called.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="each"><b>each</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.each {| popmail | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Calls the associated block once for each e-mail stored on the
server, passing in the corresponding <code>POPMail</code> object.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="finish"><b>finish</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.finish -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Closes the pop connection. Some servers require that a
connection is closed before they honor actions such as deleting
mail. Returns <code>false</code> if the connection was never used.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="mails"><b>mails</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.mails -> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of <code>POPMail</code> objects, where each object
corresponds to an e-mail message stored on the server.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.start( <i>user</i>, <i>password</i> )<br><i>aSession</i>.start( <i>user</i>, <i>password</i> ) {| pop | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Establishes a connection to the pop server, using the supplied
username and password. Fetches a list of mail held on the server,
which may be accessed using the <code>POP3#mails</code> and
<code>POP3#each</code> methods. In block form, passes <i>aSession</i> to the
block, and closes the connection using <code>finish</code> when the
block terminates.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::APOP</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Net::POP3</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#start"><i>start</i></a> <p></p><hr>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.start( <i>user</i>, <i>password</i>
)
</td></tr><td></td><td>
<P></P>
Establishes a connection to the APOP server.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::POPMail</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#all"><i>all</i></a> <a href="#delete"><i>delete</i></a> <a href="#delete_oh"><i>delete!</i></a> <a href="#header"><i>header</i></a> <a href="#size"><i>size</i></a> <a href="#top"><i>top</i></a> <a href="#uidl"><i>uidl</i></a> <p></p><hr>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="all"><b>all</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.all -> <i>aString</i><br><i>aSession</i>.all( <i>dest</i> )<br><i>aSession</i>.all {| aString | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Fetches the corresponding e-mail from the server. With no argument
or associated block, returns the e-mail as a string. With an
argument but no block, appends the e-mail to <i>dest</i> by
invoking <i>dest</i><code><<</code> for each line in the e-mail. With
an associated block, invokes the block once for each line in the e-mail.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="delete"><b>delete</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.delete
</td></tr><td></td><td>
<P></P>
Deletes the e-mail from the server.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="delete_oh"><b>delete!</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.delete!
</td></tr><td></td><td>
<P></P>
Synonym for <code>POPMail#delete</code>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="header"><b>header</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.header -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the header lines for the corresponding e-mail message.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="size"><b>size</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.size -> <i>aFixnum</i>
</td></tr><td></td><td>
<P></P>
Returns the size in bytes of the corresponding e-mail.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="top"><b>top</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.top( <i>lines</i> ) -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the header lines, plus <i>lines</i> message lines for the
corresponding e-mail message.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="uidl"><b>uidl</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.uidl -> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns the server-specific unique identifier for the
corresponding e-mail.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::SMTP</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Net::Protocol</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#start">start</a> <a href="#ready"><i>ready</i></a> <a href="#sendmail"><i>sendmail</i></a> <a href="#start"><i>start</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/smtp'
<P></P>
# --- Send using class methods
msg = [ "Subject: Test\n", "\n", "Now is the time\n" ]
Net::SMTP.start do |smtp|
smtp.sendmail( msg, 'dave@localhost', ['dave'] )
end
<P></P>
# --- Send using SMTP object and an adaptor
smtp = Net::SMTP.new
smtp.start('pragprog.com')
smtp.ready('dave@localhost', 'dave') do |a|
a.write "Subject: Test1\r\n"
a.write "\r\n"
a.write "And so is this"
end
</pre></td></tr></table>
<P></P>
The <code>net/smtp</code> library provides a simple client to send
electronic mail using the Simple Mail Transfer Protocol (SMTP).
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Net::SMTP.new( <i>server</i>='localhost',
<i>port</i>=25 ) -> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Returns a new <code>SMTP</code> object connected to the given server and
port.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
Net::SMTP.start( <i>server</i>='localhost', <i>port</i>=25,
<i>domain</i>=ENV['HOSTNAME'], <i>acct</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>, <i>authtype</i>=:cram_md5 ) -> <i>aSession</i><br>Net::SMTP.start( <i>server</i>='localhost', <i>port</i>=25,
<i>domain</i>=ENV['HOSTNAME'], <i>acct</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>, <i>authtype</i>=:cram_md5 )
{| smtp | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>Net::SMTP.new(<i>server</i>,
<i>port</i>).start(...)</code>. For an explanation of the remainder
of the parameters, see the instance method <code>Net_SMTP#start</code>.
Creates a new <code>SMTP</code> object. The <i>domain</i> parameter will be
used in the initial <code>HELO</code> or <code>EHLO</code> transaction with the
SMTP server. In the block form, the <i>smtp</i> object is passed
into the block. When the block terminates, the session is closed.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="ready"><b>ready</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.ready( <i>from</i>, <i>to</i> )
{| anAdaptor | block }
<P></P>
</td></tr><td></td><td>
<P></P>
Equivalent to <code>sendmail(from, to) { ...}</code>.
Sends header and body lines to the sendmail server. The
<i>from</i> parameter is used as the sender's name in the
<code>MAIL FROM:</code> command, and the <i>to</i> is either a string or
an array of strings containing the recipients
for the <code>RCPT TO:</code> command.
The block is passed an adaptor object. Lines are
sent to the server by calling the adaptor's <code>write</code> method. The
terminating '.' and <code>QUIT</code> are sent automatically.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="sendmail"><b>sendmail</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.sendmail( <i>src</i>, <i>from</i>, <i>to</i> )
</td></tr><td></td><td>
<P></P>
Sends header and body lines to the sendmail server. The
<i>from</i> parameter is used as the sender's name in the
<code>MAIL FROM:</code> command, and <i>to</i> is either a string or an
array of strings containing the
recipients for the <code>RCPT TO:</code> command.
Lines to be sent are fetched by invoking <i>src</i><code>.each</code>. The
terminating '.' and <code>QUIT</code> are sent automatically.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="start"><b>start</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.start( <i>domain</i>=ENV['HOSTNAME'], <i>acct</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>, <i>authtype</i>=:cram_md5 ) -> <code>true</code> or <code>false</code><br><i>aSession</i>.start( <i>domain</i>=ENV['HOSTNAME'], <i>acct</i>=<code>nil</code>,
<i>passwd</i>=<code>nil</code>, <i>authtype</i>=:cram_md5 )
{| smtp | block }
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Starts an SMTP session by connecting to the given domain
(host). If <i>acct</i> and <i>passwd</i> are given,
authentication will be attempted using the given
authentication type (<code>:plain</code> or <code>:cram_md5</code>). If a
block is supplied, it will be invoked with <i>aSession</i> as a
parameter. The connection will be closed when the block terminates.
<P></P>
</td></table>
<P></P>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class Net::Telnet</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">[Socket]</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#binmode"><i>binmode</i></a> <a href="#binmode_eq"><i>binmode=</i></a> <a href="#cmd"><i>cmd</i></a> <a href="#login"><i>login</i></a> <a href="#print"><i>print</i></a> <a href="#telnetmode"><i>telnetmode</i></a> <a href="#telnetmode_eq"><i>telnetmode=</i></a> <a href="#waitfor"><i>waitfor</i></a> <a href="#write"><i>write</i></a> <p></p><hr>
<P></P>
Connect to a <code>localhost</code>, run the ``date'' command, and disconnect.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="500">
<tr>
<td colspan="3" valign="top"><code>require 'net/telnet'</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code></code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>tn = Net::Telnet.new({})</code></td>
</tr>
<tr>
<td colspan="3" valign="top"><code>tn.login "guest", "secret"</code></td>
</tr>
<tr>
<td valign="top"><code>tn.cmd "date"</code></td>
<td valign="top"></td>
<td valign="top"><code>"date\r\nSun Mar 4 23:26:41 CST 2001\n\r> "</code></td>
</tr>
</table>
<P></P>
<P></P>
Monitor output as it occurs. We associate a block with each of the
library calls; this block is called whenever data becomes
available from the host.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/telnet'
<P></P>
tn = Net::Telnet.new({}) { |str| print str }
tn.login("guest", "secret") { |str| print str }
tn.cmd("date") { |str| print str }
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Trying localhost...
Connected to localhost.
Welcome to SuSE Linux 6.2 (i386) - Kernel 2.2.10-smp (pts/12).
login: guest
Password:
Last login: Sun Mar 4 23:26:41 from localhost
> date
Sun Mar 4 23:26:42 CST 2001
>
</pre></td></tr></table>
<P></P>
Get the time from an NTP server.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require 'net/telnet'
tn = Net::Telnet.new('Host' => 'time.nonexistent.org',
'Port' => 'time',
'Timeout' => 60,
'Telnetmode' => false)
atomicTime = tn.recv(4).unpack('N')[0]
puts "Atomic time: " + Time.at(atomicTime - 2208988800).to_s
puts "Local time: " + Time.now.to_s
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
Atomic time: Sun Mar 04 23:26:34 CST 2001
Local time: Sun Mar 04 23:26:43 CST 2001
</pre></td></tr></table>
<P></P>
The <code>net/telnet</code> library provides a complete implementation of a
telnet client and includes features that make it a convenient
mechanism for interacting with non-telnet services.
<P></P>
Although the class description that follows indicates that
<code>Net::Telnet</code> is a subclass of class <code>Socket</code>, this is a lie.
In reality, the class delegates to <code>Socket</code>. The net effect is the
same: the methods of <code>Socket</code> and its parent, class <code>IO</code>, are
available through <code>Net::Telnet</code> objects.
<P></P>
The methods <code>new</code>, <code>cmd</code>, <code>login</code>, and
<code>waitfor</code> take an optional block. If present, the block is
passed output from the server as it is received by the routine.
This can be used to provide realtime output, rather than waiting for
(for example) a login to complete before displaying the server's
response.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
Net::Telnet.new( <i>options</i> ) -> <i>aSession</i><br>Net::Telnet.new( <i>options</i> ) {| str | block }
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Connects to a server. <i>options</i> is a
<code>Hash</code> with zero or more of the following:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Option</b></td>
<td valign="top"><b>Default</b></td>
<td valign="top"><b>Meaning</b></td>
</tr>
<tr>
<td valign="top"><code>Binmode</code></td>
<td valign="top"><code>false</code></td>
<td valign="top">If true, no end-of-line
processing will be performed.</td>
</tr>
<tr>
<td valign="top"><code>Host</code></td>
<td valign="top">localhost</td>
<td valign="top">Name or address of server's host.</td>
</tr>
<tr>
<td valign="top"><code>Port</code></td>
<td valign="top">23</td>
<td valign="top">Name or number of service to call.</td>
</tr>
<tr>
<td valign="top"><code>Prompt</code></td>
<td valign="top"><code>/[$%#>]/</code></td>
<td valign="top">Pattern that matches the host's
prompt.</td>
</tr>
<tr>
<td valign="top"><code>Telnetmode</code></td>
<td valign="top"><code>true</code></td>
<td valign="top">If <code>false</code>, ignore the
majority of telnet embedded escape sequences. Used when
talking with a non-telnet server.</td>
</tr>
<tr>
<td valign="top"><code>Timeout</code></td>
<td valign="top">10</td>
<td valign="top">Time in seconds to wait for a server
response (both during connection and during regular data
transmission).</td>
</tr>
<tr>
<td valign="top"><code>Waittime</code></td>
<td valign="top">0</td>
<td valign="top">Time to wait for prompt to appear in
received data stream.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="binmode"><b>binmode</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.binmode -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the current value of the <code>Binmode</code> flag.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="binmode_eq"><b>binmode=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.binmode = <code>true</code> or <code>false</code></td></tr><td></td><td>
<P></P>
Sets the <code>Binmode</code> flag, returning the new value.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="cmd"><b>cmd</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.cmd( <i>options</i> ) -> <i>aString</i><br><i>aSession</i>.cmd( <i>options</i> ) {| str | block }
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Sends a string to the server and waits (using a timeout) for a
string that matches a pattern to be returned by the server. If
the parameter is not a <code>Hash</code>, it is sent as a string to the server, and
the pattern to match and the timeout are the <code>Prompt</code> and
<code>Timeout</code> options given when <i>aSession</i> was created.
If <i>options</i> is a <code>Hash</code>, then <i>options['String']</i> is
sent to the server. <i>options['Match']</i> may be used to
override the class <code>Prompt</code> parameter, and <i>options['Timeout']</i>
the timeout. The method returns the complete server response.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="login"><b>login</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.login( <i>options</i>,
<i>password</i>=nil ) -> <i>aString</i><br><i>aSession</i>.login( <i>options</i>,
<i>password</i>=nil ) {| str | block }
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
If <i>options</i> is a <code>Hash</code>, a username is taken from
<i>options['Name']</i> and a password from
<i>options['Password']</i>; otherwise, <i>options</i> is assumed to
be the username, and <i>password</i> the password. The method
waits for the server to send the string matching the pattern
<code>/login[:<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">]*\z/</code> and sends the username. If a
password is given, it then waits for the server to send
<code>/Password[:<img src="visible_space.gif" width="15" height="10" align="bottom" alt="[visible space]">]*\z/</code> and sends the password. The
method returns the full server response.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="print"><b>print</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.print( <i>aString</i> )
</td></tr><td></td><td>
<P></P>
Sends <i>aString</i> to the server, honoring <code>Telnetmode</code>,
<code>Binarymode</code>, and any additional modes negotiated with the
server.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="telnetmode"><b>telnetmode</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.telnetmode -> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns the current value of the <code>Telnetmode</code> flag.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="telnetmode_eq"><b>telnetmode=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.telnetmode= <code>true</code> or <code>false</code></td></tr><td></td><td>
<P></P>
Sets the <code>Telnetmode</code>
flag, returning the new value.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="waitfor"><b>waitfor</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.waitfor( <i>options</i> ) -> <i>aString</i><br><i>aSession</i>.waitfor( <i>options</i> ) {| str | block }
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Waits for the server to respond with a string that matches a
string or pattern. If <i>options</i> is not a <code>Hash</code>, it
is compared against the cumulative server output as that output
is received using <i>options.</i><code>===</code>. It is likely that you will
want to use a regular expression in this case.
<P></P>
If <i>options</i> is a <code>Hash</code>, then <i>options['Match']</i>,
<i>options['Prompt']</i>, or <i>options['String']</i> provides the
match. In the latter case, the string will be converted to a
regular expression before being used. <i>options</i> may also
include the keys ``Timeout'' and ``Waittime'' to override the
class options of the same names.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="write"><b>write</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.write( <i>aString</i> )
</td></tr><td></td><td>
<P></P>
Writes <i>aString</i> to the server with no translation.
<P></P>
</td></table>
<P></P>
<P></P>
<h2>CGI Development</h2>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class CGI</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#escape">escape</a> <a href="#escapeElement">escapeElement</a> <a href="#escapeHTML">escapeHTML</a> <a href="#new">new</a> <a href="#parse">parse</a> <a href="#pretty">pretty</a> <a href="#rfc1123_date">rfc1123_date</a> <a href="#unescape">unescape</a> <a href="#unescapeElement">unescapeElement</a> <a href="#unescapeHTML">unescapeHTML</a> <a href="#_ob_cb"><i>[ ]</i></a> <a href="#cookies"><i>cookies</i></a> <a href="#has_key_qm"><i>has_key?</i></a> <a href="#header"><i>header</i></a> <a href="#keys"><i>keys</i></a> <a href="#out"><i>out</i></a> <a href="#params"><i>params</i></a> <p></p><hr>
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
require "cgi"
cgi = CGI.new("html3") # add HTML generation methods
cgi.out {
CGI.pretty (
cgi.html {
cgi.head { cgi.title{"TITLE"} } +
cgi.body {
cgi.form {
cgi.textarea("get_text") +
cgi.br +
cgi.submit
} +
cgi.h1 { "This is big!" } +
cgi.center { "Jazz Greats of the 20" +
cgi.small {"th"} + " century" + cgi.hr
} + cgi.p + cgi.table ('BORDER' => '5') {
cgi.tr { cgi.td {"Artist"} + cgi.td {"Album"} } +
cgi.tr { cgi.td {"Davis, Miles"} +
cgi.td {"Kind of Blue"} }
}
}
}
) # CGI.pretty is a method call, not a block
}
</pre></td></tr></table>
<P></P>
(The output of this script is shown in Figure 26.2 on page 503.)
<P></P>
The <code>CGI</code> class provides support for programs used as a Web server
CGI (Common Gateway Interface) script. It contains several
methods for accessing fields in a CGI form, manipulating ``cookies'' and
the environment, and outputting formatted HTML.
<P></P>
Since environment variables contain a lot of useful information for
a CGI script, <code>CGI</code> makes accessing them very easy---environment
variables are accessible as attributes of <code>CGI</code> objects. For
instance, <code>cgi.auth_type</code> returns the value of
<code>ENV["AUTH_TYPE"]</code>. To create the method name, the environment
variable name is translated to all lowercase, and the
``<code>HTTP_</code>'' prefix is stripped off. Thus,
<code>HTTP_USER_AGENT</code> would be available as the method
<code>user_agent</code>.
<P></P>
Cookies are represented using a separate object of class
<code>CGI::Cookie</code>, containing the following accessors:
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Accessor</b></td>
<td valign="top"><b>Description</b></td>
</tr>
<tr>
<td valign="top"><code>name</code></td>
<td valign="top">Name of this cookie</td>
</tr>
<tr>
<td valign="top"><code>value</code></td>
<td valign="top">Array of values</td>
</tr>
<tr>
<td valign="top"><code>path</code></td>
<td valign="top">Path (optional)</td>
</tr>
<tr>
<td valign="top"><code>domain</code></td>
<td valign="top">Domain (optional)</td>
</tr>
<tr>
<td valign="top"><code>expires</code></td>
<td valign="top">Time of expiry, defaults to <a href="ref_c_time.html#now"><code>Time::now</code></a> (optional)</td>
</tr>
<tr>
<td valign="top"><code>secure</code></td>
<td valign="top"><code>true</code> for a secure cookie</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>Figure not available...</td></tr></table>
<P></P>
You create a cookie object using <code>CGI_Cookie#new</code>, which takes
as arguments the accessors listed above, or <code>CGI_Cookie#parse</code>,
which takes an encoded string and returns a cookie object.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="escape"><b>escape</b></a></font></td><td bgcolor="#ffaaaa">
CGI.escape( <i>aString</i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a URL-encoded string made from the given argument, where unsafe
characters (not alphanumeric, ``_'', ``-'', or ``.'') are encoded
using ``%xx'' escapes.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="escapeElement"><b>escapeElement</b></a></font></td><td bgcolor="#ffaaaa">
CGI.escapeElement( <i>aString</i>
<i>[</i>, <i>elements</i><i>]<sup>*</sup></i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a string made from the given argument with certain
HTML-special characters escaped. The HTML elements given in
<i>elements</i> will be escaped; other HTML elements will not
be affected.
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
print CGI::escapeElement('<BR><A HREF="url"></A><P>', "A", "IMG")
</pre></td></tr></table>
<em>produces:</em>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3" width="400"><tr><td><pre>
<BR>&lt;A HREF=&quot;url&quot;&gt;&lt;/A&gt;<P>
</pre></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="escapeHTML"><b>escapeHTML</b></a></font></td><td bgcolor="#ffaaaa">
CGI.escapeHTML( <i>aString</i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a string made from the given argument with
HTML-special characters (such as
``<code>&</code>'',``<code>"</code>'',``<code><</code>'',``<code>></code>'') quoted using
``<code>&amp;</code>'', ``<code>&quot;</code>'', ``<code>&lt;</code>'',
``<code>&gt;</code>'', and so on.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
CGI.new( <i>[</i><i>aString</i><i>]<sup>*</sup></i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Returns a new <code>CGI</code> object. If HTML output is required, the
desired standards level must be given in <i>aString</i>
(otherwise, no output routines will be created). The level may be
one of:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>String</b></td>
<td valign="top"><b>Standards Level</b></td>
<td valign="top"><b>String</b></td>
<td valign="top"><b>Standards Level</b></td>
</tr>
<tr>
<td valign="top">``html3''</td>
<td valign="top">HTML 3.2</td>
<td valign="top">``html4''</td>
<td valign="top">HTML 4.0 Strict</td>
</tr>
<tr>
<td valign="top">``html4Tr''</td>
<td valign="top">HTML 4.0 Transitional</td>
<td valign="top">``html4Fr''</td>
<td valign="top">HTML 4.0 Frameset</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="parse"><b>parse</b></a></font></td><td bgcolor="#ffaaaa">
CGI.parse( <i>aString</i> )
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Parses a query string and returns
a hash of its <em>key-value</em> pairs.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="pretty"><b>pretty</b></a></font></td><td bgcolor="#ffaaaa">
CGI.pretty( <i>anHTMLString</i>,
<i>aLeaderString</i>=" " )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Formats the given <i>anHTMLString</i> in a nice, readable
format, optionally prefixing each line with
<i>aLeaderString</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="rfc1123_date"><b>rfc1123_date</b></a></font></td><td bgcolor="#ffaaaa">
CGI.rfc1123_date( <i>aTime</i> )
-> <i>aString</i>
</td></tr><td></td><td>
<P></P>
Returns a string representing the given time according to
RFC 1123 (for instance, <code>Mon, 1 Jan 2001 00:00:00 GMT</code>).
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="unescape"><b>unescape</b></a></font></td><td bgcolor="#ffaaaa">
CGI.unescape( <i>aString</i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a string containing ``unsafe'' characters made from
the given URL-encoded argument, where unsafe characters were
encoded using ``%'' escapes.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="unescapeElement"><b>unescapeElement</b></a></font></td><td bgcolor="#ffaaaa">
CGI.unescapeElement( <i>aString</i>
<i>[</i>, <i>elements</i><i>]<sup>*</sup></i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a string with the selected escaped HTML elements
expanded to the actual characters.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="unescapeHTML"><b>unescapeHTML</b></a></font></td><td bgcolor="#ffaaaa">
CGI.unescapeHTML( <i>aString</i> )
-> <i>aNewString</i>
</td></tr><td></td><td>
<P></P>
Returns a string made from the given argument with
HTML-special quoted characters expanded to the actual
characters.
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>[ <i>[</i><i>aString</i><i>]<sup>+</sup></i> ]
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns the values of the given field names from the CGI
form in an <code>Array</code>. See the note on multipart forms on page 507.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="cookies"><b>cookies</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.cookies
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Returns a new <code>Hash</code> object containing <em>key-value</em>
pairs of cookie keys and values.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="has_key_qm"><b>has_key?</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.has_key( <i>aString</i> )
-> <code>true</code> or <code>false</code>
</td></tr><td></td><td>
<P></P>
Returns <code>true</code> if the form contains a field named
<i>aString</i>.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="header"><b>header</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.header( <i>aContentType</i>="text/html" )
-> <i>aString</i> <br><i>aSession</i>.header( <i>aHash</i> )
-> <i>aString</i> <br>
</td></tr><td></td><td>
<P></P>
Returns a string containing the given headers
(in the <code>MOD_RUBY</code> environment, the resulting header is
sent immediately instead). If a hash is given as an
argument, then the <em>key-value</em> pairs will be used to
generate headers.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="keys"><b>keys</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.keys
-> <i>anArray</i>
</td></tr><td></td><td>
<P></P>
Returns an array of all existing field names for the form.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="out"><b>out</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.out( <i>aContentType</i>="text/html" ) { block }
-> <code>nil</code> <br><i>aSession</i>.out( <i>aHash</i> ) { block }
-> <code>nil</code> <br>
</td></tr><td></td><td>
<P></P>
Generates HTML output using the results of the block as the
content. Headers are generated as with <code>CGI#header</code>.
See the example at the start of this section.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="params"><b>params</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.params
-> <i>aHash</i>
</td></tr><td></td><td>
<P></P>
Returns a new <code>Hash</code> object containing <em>key-value</em>
pairs of field names and values from the form.
<P></P>
</td></table>
<P></P>
<P></P>
<h3>HTML Output Methods</h3>
In addition, <code>CGI</code> supports the following HTML output methods. Each
of these methods is named after the corresponding HTML feature (or
close to it). Those tags that require content (such as
<code>blockquote</code>) take an optional block; the block should return a
<code>String</code> that will be used as the content for the feature. These
methods may take arguments as indicated, or as a hash with the given
names as keys.
<P></P>\<hr>
<br><code>a</code>( <em><i>url</i></em> )<br><code>a</code>( <em><code>HREF</code></em> => )<P></P>
<P></P>
<br><code>base</code>( <em><i>url</i></em> )<br><code>base</code>( <em><code>HREF</code></em> => )<P></P>
<P></P>
<br><code>blockquote</code>( <em><i>cite=""</i></em> ) { <em>aString</em> }<br><code>blockquote</code>( <em><code>CITE</code></em> => ) { <em>aString</em> }<P></P>
<P></P>
<br><code>caption</code>( <em><i>align=nil</i></em> ) { <em>aString</em> }<br><code>caption</code>( <em><code>ALIGN</code></em> => ) { <em>aString</em> }<P></P>
<P></P>
<br><code>checkbox</code>( <em><i>name</i>=<code>nil</code>, <i>value</i>=<code>nil</code>, <i>checked</i>=<code>nil</code></em> )<br><code>checkbox</code>( <em><code>NAME</code>, <code>VALUE</code>, <code>CHECKED</code></em> => )<P></P>
<P></P>
<br><code>checkbox_group</code>( <em><i>name</i>=<code>nil</code>, <i>[</i><i>items</i><i>]<sup>+</sup></i></em> )<br><code>checkbox_group</code>( <em> <code>NAME</code>, <code>VALUES</code></em> => )<P></P>
<P></P>
<blockquote>Items may be individual <code>String</code> names, or any of:
an array of [ <i>name</i>, <i>checked</i> ],
an array of [ <i>value</i>, <i>name</i> ],
or an array of [ <i>value</i>, <i>name</i>, <i>checked</i> ].
The value for the hash key VALUES should be an array of these items.
</blockquote>
<P></P>
<br><code>file_field</code>( <em><i>name</i>="", <i>size</i>=20, <i>maxlength</i>=<code>nil</code></em> )<br><code>file_field</code>( <em> <code>NAME</code>, <code>SIZE</code>, <code>MAXLENGTH</code></em> => )<P></P>
<P></P>
<br><code>form</code>( <em><i>method</i>="post", <i>action</i>=<code>nil</code>,
<i>enctype</i>="application/x-www-form-urlencoded"</em> ) { <em>aStr</em> }<br><code>form</code>( <em> <code>METHOD</code>, <code>ACTION</code>, <code>ENCTYPE</code></em> => ) { <em>aStr</em> }<P></P>
<P></P>
<br><code>hidden</code>( <em><i>name</i>="", <i>value</i>=<code>nil</code></em> )<br><code>hidden</code>( <em><code>NAME</code>, <code>VALUE</code></em> => )<P></P>
<P></P>
<br><code>html</code>( <em></em> ) { <em>aString</em> }<br><code>html</code>( <em><code>PRETTY</code>, <code>DOCTYPE</code></em> => ) { <em>aString</em> }<P></P>
<P></P>
<br><code>img_button</code>( <em><i>src</i>="", <i>name</i>=<code>nil</code>, <i>alt</i>=<code>nil</code></em> )<br><code>img_button</code>( <em> <code>SRC</code>, <code>NAME</code>, <code>ALT</code></em> => )<P></P>
<P></P>
<br><code>img</code>( <em><i>src</i>="", <i>alt</i>="", <i>width</i>=<code>nil</code>, <i>height</i>=<code>nil</code></em> )<br><code>img</code>( <em> <code>SRC</code>, <code>ALT</code>, <code>WIDTH</code>, <code>HEIGHT</code></em> => )<P></P>
<P></P>
<br><code>multipart_form</code>( <em><i>action</i>=<code>nil</code>, <i>enctype</i>="multipart/form-data"</em> ) { <em>aString</em> }<br><code>multipart_form</code>( <em><code>METHOD</code>, <code>ACTION</code>, <code>ENCTYPE</code></em> => ) { <em>aString</em> }<P></P>
<P></P>
<br><code>password_field</code>( <em><i>name</i>="", <i>value</i>=<code>nil</code>, <i>size</i>=40,
<i>maxlength</i>=<code>nil</code></em> )<br><code>password_field</code>( <em><code>NAME</code>, <code>VALUE</code>, <code>SIZE</code>, <code>MAXLENGTH</code></em> => )<P></P>
<P></P>
<br><code>popup_menu</code>( <em><i>name</i>="", <i>items</i></em> )<br><code>popup_menu</code>( <em> <code>NAME</code>, <code>SIZE</code>, <code>MULTIPLE</code>, <code>VALUES</code> <em>(array of items)</em></em> => )<P></P>
<P></P>
<blockquote>Items may be individual <code>String</code> names, or any of:
an array of [ <i>name</i>, <i>selected</i> ],
an array of [ <i>value</i>, <i>name</i> ],
or an array of [ <i>value</i>, <i>name</i>, <i>selected</i> ].
The value for the hash key VALUES should be an array of these items.
</blockquote>
<P></P>
<br><code>radio_button</code>( <em><i>name</i>="", <i>value</i>=<code>nil</code>, <i>checked</i>=<code>nil</code></em> )<br><code>radio_button</code>( <em> <code>NAME</code>, <code>VALUE</code>, <code>CHECKED</code></em> => )<P></P>
<P></P>
<br><code>radio_group</code>( <em><i>name</i>="", <i>items</i></em> )<br><code>radio_group</code>( <em><code>NAME</code>, <code>VALUES</code>
<em>(array of items)</em></em> => )<P></P>
<P></P>
<blockquote>Items may be individual <code>String</code> names, or any of:
an array of [ <i>name</i>, <i>selected</i> ],
an array of [ <i>value</i>, <i>name</i> ],
or an array of [ <i>value</i>, <i>name</i>, <i>selected</i> ].
The value for the hash key VALUES should be an array of these items.
</blockquote>
<P></P>
<br><code>reset</code>( <em><i>value</i>=<code>nil</code>, <i>name</i>=<code>nil</code></em> )<br><code>reset</code>( <em><code>VALUE</code>, <code>NAME</code></em> => )<P></P>
<P></P>
<br><code>scrolling_list</code>( <em><em>alias for</em> popup_menu</em> )<br><code>scrolling_list</code>( <em></em> => )<P></P>
<P></P>
<br><code>submit</code>( <em><i>value</i>=<code>nil</code>, <i>name</i>=<code>nil</code></em> )<br><code>submit</code>( <em><code>VALUE</code>, <code>NAME</code></em> => )<P></P>
<P></P>
<br><code>text_field</code>( <em><i>name</i>="", <i>value</i>=<code>nil</code>, <i>size</i>=40,
<i>maxlength</i>=<code>nil</code></em> )<br><code>text_field</code>( <em><code>NAME</code>, <code>VALUE</code>, <code>SIZE</code>, MAXLENGTH</em> => )<P></P>
<P></P>
<br><code>textarea</code>( <em><i>name</i>="", <i>cols</i>=70, <i>rows</i>=10</em> )<br><code>textarea</code>( <em> <code>NAME</code>, <code>COLS</code>, <code>ROWS</code></em> => )<P></P>
<P></P>
\<hr>
<P></P>
In addition, all HTML tags are supported as methods, including
<code>title</code>, <code>head</code>, <code>body</code>, <code>br</code>, <code>pre</code>, and so on.
The block given to the method must return a <code>String</code>, which will be
used as the content for that tag type. Not all tags require content:
<code><P></code>, for example, does not.
The available tags vary according to the supported HTML level---Table
26.1 on page 507 lists the complete set. For these methods, you
can pass in a hash with attributes for the given tag. For instance, you
might pass in <code>'BORDER'=>'5'</code> to the <code>table</code> method to set the
border width of the table.
<table border="2" width="500" bgcolor="#ffe0e0"><tr><td>
<b>HTML tags available as methods</b>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top">{<b>HTML 3</b>}</td>
</tr>
<tr>
<td valign="top">a address applet area b base basefont big blockquote body br caption
center cite code dd dfn dir div dl dt em font form h1 h2 h3 h4 h5 h6
head hr html i img input isindex kbd li link listing map menu meta
ol option p param plaintext pre samp script select small strike
strong style sub sup table td textarea th title tr tt u ul var xmp</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="3"><img src="dot.gif" width="1" height="1"></td></tr><tr>
<td valign="top">{<b>HTML 4</b>}</td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top">a abbr acronym address area b base bdo big blockquote body br
button caption cite code col colgroup dd del dfn div dl dt em
fieldset form h1 h2 h3 h4 h5 h6 head hr html i img input ins kbd
label legend li link map meta noscript object ol optgroup option p
param pre q samp script select small span strong style sub sup
table tbody td textarea tfoot th thead title tr tt ul var</td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top">{<b>HTML 4 Transitional</b>}</td>
</tr>
<tr>
<td valign="top">a abbr acronym address applet area b base basefont bdo big
blockquote body br button caption center cite code col colgroup dd
del dfn dir div dl dt em fieldset font form h1 h2 h3 h4 h5 h6 head
hr html i iframe img input ins isindex kbd label legend li link map
menu meta noframes noscript object ol optgroup option p param pre q
s samp script select small span strike strong style sub sup table
tbody td textarea tfoot th thead title tr tt u ul var</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="3"><img src="dot.gif" width="1" height="1"></td></tr><tr>
<td valign="top"></td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top">frame frameset</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></tr></table>
<P></P>
<h3>Multipart Form Values</h3>
<P></P>
When dealing with a multipart form, the array returned by <code>CGI#[]</code>
is composed of objects of class <code>Tempfile</code>, with the following
dynamically added methods:
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Method</b></td>
<td valign="top"><b>Description</b></td>
</tr>
<tr>
<td valign="top"><code>read</code></td>
<td valign="top">Body</td>
</tr>
<tr>
<td valign="top"><code>local_path</code></td>
<td valign="top">Path to local file containing the content</td>
</tr>
<tr>
<td valign="top"><code>original_filename</code></td>
<td valign="top">Original filename of the content</td>
</tr>
<tr>
<td valign="top"><code>content_type</code></td>
<td valign="top">Content type</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
<table><tr><td height="20"><img src="dot.gif" width="1" height="20"></td></tr></table><table border="0" width="100%" bgcolor="660066" cellpadding="10"><tr><td valign="center"><font color="white" size="7">class CGI::Session</font></td><td><table border="0"><tr><td><font color="white">
Parent:
</font></td><td><font color="white">Object</font></td></tr><tr><td><font color="white">
Version:
</font></td><td><font color="white">
1.6
</font></td></tr></table></td></tr></table><p></p><H3>Index:</H3><a href="#new">new</a> <a href="#_ob_cb"><i>[ ]</i></a> <a href="#_ob_cb_eq"><i>[ ]=</i></a> <a href="#delete"><i>delete</i></a> <a href="#update"><i>update</i></a> <p></p><hr>
<P></P>
A <code>CGI::Session</code> maintains a persistent state for web users in a
CGI environment. Sessions may be memory-resident or may be stored
on disk. See the discussion on page 148 for details.
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">class methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="new"><b>new</b></a></font></td><td bgcolor="#ffaaaa">
CGI::Session.new( <i>aCgi</i>, <i>[</i><i>aHash</i><i>]<sup>*</sup></i> )
-> <i>aSession</i>
</td></tr><td></td><td>
<P></P>
Returns a new session object for the <code>CGI</code> query. Options
that may be given in <i>aHash</i> include:
<P></P>
<table bgcolor="#fff0f0" cellspacing="0" border="0" cellpadding="3">
<tr bgcolor="#ff9999">
<td valign="top"><b>Option</b></td>
<td valign="top"><b>Description</b></td>
</tr>
<tr>
<td valign="top"><code>session_key</code></td>
<td valign="top">Name of CGI key for session
identification.</td>
</tr>
<tr>
<td valign="top"><code>session_id</code></td>
<td valign="top">Value of session id.</td>
</tr>
<tr>
<td valign="top"><code>new_session</code></td>
<td valign="top">If <code>true</code>, create a new session
id for this session. If <code>false</code>, use an
existing session identified by <code>session_id</code>. If
omitted, use an existing session if available,
otherwise create a new one.</td>
</tr>
<tr>
<td valign="top"><code>database_manager</code></td>
<td valign="top">Class to use to save sessions; may be
<code>CGI::Session::FileStore</code> or
<code>CGI::Session::MemoryStore</code>
(or user defined if you're
brave). Default is <code>FileStore</code>.</td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top"><code>tmpdir</code></td>
<td valign="top">For <code>FileStore,</code> directory for session files.</td>
</tr>
<tr bgcolor="#ff9999">
<td valign="top"><code>prefix</code></td>
<td valign="top">For <code>FileStore,</code> prefix of session filenames.</td>
</tr>
<tr><td colspan="9" bgcolor="#ff9999" height="2"><img src="dot.gif" width="1" height="1"></td></tr></table>
<P></P>
</td></table>
<P></P>
<table border="0" width="100%" cellpadding="10"><tr><td valign="center" colspan="2" bgcolor="990066"><font color="white" size="6">instance methods
</font></td></tr><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb"><b>[ ]</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>[ <i>aKey</i> ]
-> <i>aValue</i>
</td></tr><td></td><td>
<P></P>
Returns the value for the given key.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="_ob_cb_eq"><b>[ ]=</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>[ <i>aKey</i> ] = <i>aValue</i>
-> <i>aValue</i>
</td></tr><td></td><td>
<P></P>
Sets the value for the given key.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="delete"><b>delete</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.delete
<P></P>
</td></tr><td></td><td>
<P></P>
Calls the <code>delete</code> method of the underlying database
manager. For <code>FileStore</code>, deletes the physical file
containing the session.
For <code>MemoryStore</code>, removes the session from memory.
<P></P>
</td><tr><td valign="center" bgcolor="#ff9999"><font size="4"><a name="update"><b>update</b></a></font></td><td bgcolor="#ffaaaa">
<i>aSession</i>.update
<P></P>
</td></tr><td></td><td>
<P></P>
Calls the <code>update</code> method of the underlying database
manager. For <code>FileStore</code>, writes the session data out to
disk. Has no effect with <code>MemoryStore</code>.
<P></P>
</td></table>
<P></P>
<P></P>
<p></p><hr><table bgcolor="#a03030" cellpadding="10" border="0" cellspacing="0"><tr><td width="33%" align="left"><a class="subheader" href="lib_patterns.html">Previous <</a></td><td width="33%" align="center" valign="middle"><a class="subheader" href="index.html">Contents ^</a><br></td><td width="33%" align="right"><a class="subheader" href="lib_windows.html">Next ></a><br></td></tr></table><p></p><font size="-1">Extracted from the book "Programming Ruby -
The Pragmatic Programmer's Guide"</font><br><font size="-3">
Copyright
©
2000 Addison Wesley Longman, Inc. Released under the terms of the
<a href="http://www.opencontent.org/openpub/">Open Publication License</a> V1.0.
<br>
This reference is available for
<a href="http://www.pragmaticprogrammer.com/ruby/downloads/book.html">download</a>.
</font></body></html>
|