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
|
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Interface documentation.
Maintainer: Itamar Shtull-Trauring
"""
from __future__ import annotations
from typing import (
TYPE_CHECKING,
Any,
AnyStr,
Callable,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
Type,
Union,
)
from zope.interface import Attribute, Interface
from twisted.python.failure import Failure
if TYPE_CHECKING:
from socket import AddressFamily
try:
from OpenSSL.SSL import (
Connection as OpenSSLConnection,
Context as OpenSSLContext,
)
except ImportError:
OpenSSLConnection = OpenSSLContext = object # type: ignore[misc,assignment]
from twisted.internet.abstract import FileDescriptor
from twisted.internet.address import IPv4Address, IPv6Address, UNIXAddress
from twisted.internet.defer import Deferred
from twisted.internet.protocol import (
ClientFactory,
ConnectedDatagramProtocol,
DatagramProtocol,
Factory,
ServerFactory,
)
from twisted.internet.ssl import ClientContextFactory
from twisted.names.dns import Query, RRHeader
from twisted.protocols.tls import TLSMemoryBIOProtocol
from twisted.python.runtime import platform
if platform.supportsThreads():
from twisted.python.threadpool import ThreadPool
else:
ThreadPool = object # type: ignore[misc, assignment]
class IAddress(Interface):
"""
An address, e.g. a TCP C{(host, port)}.
Default implementations are in L{twisted.internet.address}.
"""
### Reactor Interfaces
class IConnector(Interface):
"""
Object used to interface between connections and protocols.
Each L{IConnector} manages one connection.
"""
def stopConnecting() -> None:
"""
Stop attempting to connect.
"""
def disconnect() -> None:
"""
Disconnect regardless of the connection state.
If we are connected, disconnect, if we are trying to connect,
stop trying.
"""
def connect() -> None:
"""
Try to connect to remote address.
"""
def getDestination() -> IAddress:
"""
Return destination this will try to connect to.
@return: An object which provides L{IAddress}.
"""
class IResolverSimple(Interface):
def getHostByName(name: str, timeout: Sequence[int] = ()) -> "Deferred[str]":
"""
Resolve the domain name C{name} into an IP address.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: The callback of the Deferred that is returned will be
passed a string that represents the IP address of the
specified name, or the errback will be called if the
lookup times out. If multiple types of address records
are associated with the name, A6 records will be returned
in preference to AAAA records, which will be returned in
preference to A records. If there are multiple records of
the type to be returned, one will be selected at random.
@raise twisted.internet.defer.TimeoutError: Raised
(asynchronously) if the name cannot be resolved within the
specified timeout period.
"""
class IHostResolution(Interface):
"""
An L{IHostResolution} represents represents an in-progress recursive query
for a DNS name.
@since: Twisted 17.1.0
"""
name = Attribute(
"""
L{unicode}; the name of the host being resolved.
"""
)
def cancel() -> None:
"""
Stop the hostname resolution in progress.
"""
class IResolutionReceiver(Interface):
"""
An L{IResolutionReceiver} receives the results of a hostname resolution in
progress, initiated by an L{IHostnameResolver}.
@since: Twisted 17.1.0
"""
def resolutionBegan(resolutionInProgress: IHostResolution) -> None:
"""
A hostname resolution began.
@param resolutionInProgress: an L{IHostResolution}.
"""
def addressResolved(address: IAddress) -> None:
"""
An internet address. This is called when an address for the given name
is discovered. In the current implementation this practically means
L{IPv4Address} or L{IPv6Address}, but implementations of this interface
should be lenient to other types being passed to this interface as
well, for future-proofing.
@param address: An address object.
"""
def resolutionComplete() -> None:
"""
Resolution has completed; no further addresses will be relayed to
L{IResolutionReceiver.addressResolved}.
"""
class IHostnameResolver(Interface):
"""
An L{IHostnameResolver} can resolve a host name and port number into a
series of L{IAddress} objects.
@since: Twisted 17.1.0
"""
def resolveHostName(
resolutionReceiver: IResolutionReceiver,
hostName: str,
portNumber: int = 0,
addressTypes: Optional[Sequence[Type[IAddress]]] = None,
transportSemantics: str = "TCP",
) -> IHostResolution:
"""
Initiate a hostname resolution.
@param resolutionReceiver: an object that will receive each resolved
address as it arrives.
@param hostName: The name of the host to resolve. If this contains
non-ASCII code points, they will be converted to IDNA first.
@param portNumber: The port number that the returned addresses should
include.
@param addressTypes: An iterable of implementors of L{IAddress} that
are acceptable values for C{resolutionReceiver} to receive to its
L{addressResolved <IResolutionReceiver.addressResolved>}. In
practice, this means an iterable containing
L{twisted.internet.address.IPv4Address},
L{twisted.internet.address.IPv6Address}, both, or neither.
@param transportSemantics: A string describing the semantics of the
transport; either C{'TCP'} for stream-oriented transports or
C{'UDP'} for datagram-oriented; see
L{twisted.internet.address.IPv6Address.type} and
L{twisted.internet.address.IPv4Address.type}.
@return: The resolution in progress.
"""
class IResolver(IResolverSimple):
def query(
query: "Query", timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Dispatch C{query} to the method which can handle its type.
@param query: The DNS query being issued, to which a response is to be
generated.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupAddress(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an A record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupAddress6(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an A6 record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupIPV6Address(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an AAAA record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupMailExchange(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an MX record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupNameservers(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an NS record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupCanonicalName(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a CNAME record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupMailBox(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an MB record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupMailGroup(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an MG record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupMailRename(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an MR record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupPointer(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a PTR record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupAuthority(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an SOA record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupNull(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a NULL record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupWellKnownServices(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a WKS record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupHostInfo(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a HINFO record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupMailboxInfo(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an MINFO record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupText(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a TXT record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupResponsibility(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an RP record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupAFSDatabase(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an AFSDB record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupService(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an SRV record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupAllRecords(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an ALL_RECORD lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupSenderPolicy(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a SPF record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupNamingAuthorityPointer(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform a NAPTR record lookup.
@param name: DNS name to resolve.
@param timeout: Number of seconds after which to reissue the query.
When the last timeout expires, the query is considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances. The first element of the
tuple gives answers. The second element of the tuple gives
authorities. The third element of the tuple gives additional
information. The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
def lookupZone(
name: str, timeout: Sequence[int]
) -> "Deferred[Tuple[RRHeader, RRHeader, RRHeader]]":
"""
Perform an AXFR record lookup.
NB This is quite different from other DNS requests. See
U{http://cr.yp.to/djbdns/axfr-notes.html} for more
information.
NB Unlike other C{lookup*} methods, the timeout here is not a
list of ints, it is a single int.
@param name: DNS name to resolve.
@param timeout: When this timeout expires, the query is
considered failed.
@return: A L{Deferred} which fires with a three-tuple of lists of
L{twisted.names.dns.RRHeader} instances.
The first element of the tuple gives answers.
The second and third elements are always empty.
The L{Deferred} may instead fail with one of the
exceptions defined in L{twisted.names.error} or with
C{NotImplementedError}.
"""
class IReactorTCP(Interface):
def listenTCP(
port: int, factory: "ServerFactory", backlog: int, interface: str
) -> "IListeningPort":
"""
Connects a given protocol factory to the given numeric TCP/IP port.
@param port: a port number on which to listen
@param factory: a L{twisted.internet.protocol.ServerFactory} instance
@param backlog: size of the listen queue
@param interface: The local IPv4 or IPv6 address to which to bind;
defaults to '', ie all IPv4 addresses. To bind to all IPv4 and IPv6
addresses, you must call this method twice.
@return: an object that provides L{IListeningPort}.
@raise CannotListenError: as defined here
L{twisted.internet.error.CannotListenError},
if it cannot listen on this port (e.g., it
cannot bind to the required port number)
"""
def connectTCP(
host: str,
port: int,
factory: "ClientFactory",
timeout: float,
bindAddress: Optional[Tuple[str, int]],
) -> IConnector:
"""
Connect a TCP client.
@param host: A hostname or an IPv4 or IPv6 address literal.
@param port: a port number
@param factory: a L{twisted.internet.protocol.ClientFactory} instance
@param timeout: number of seconds to wait before assuming the
connection has failed.
@param bindAddress: a (host, port) tuple of local address to bind
to, or None.
@return: An object which provides L{IConnector}. This connector will
call various callbacks on the factory when a connection is
made, failed, or lost - see
L{ClientFactory<twisted.internet.protocol.ClientFactory>}
docs for details.
"""
class IReactorSSL(Interface):
def connectSSL(
host: str,
port: int,
factory: "ClientFactory",
contextFactory: "ClientContextFactory",
timeout: float,
bindAddress: Optional[Tuple[str, int]],
) -> IConnector:
"""
Connect a client Protocol to a remote SSL socket.
@param host: a host name
@param port: a port number
@param factory: a L{twisted.internet.protocol.ClientFactory} instance
@param contextFactory: a L{twisted.internet.ssl.ClientContextFactory} object.
@param timeout: number of seconds to wait before assuming the
connection has failed.
@param bindAddress: a (host, port) tuple of local address to bind to,
or L{None}.
@return: An object which provides L{IConnector}.
"""
def listenSSL(
port: int,
factory: "ServerFactory",
contextFactory: "IOpenSSLContextFactory",
backlog: int,
interface: str,
) -> "IListeningPort":
"""
Connects a given protocol factory to the given numeric TCP/IP port.
The connection is a SSL one, using contexts created by the context
factory.
@param port: a port number on which to listen
@param factory: a L{twisted.internet.protocol.ServerFactory} instance
@param contextFactory: an implementor of L{IOpenSSLContextFactory}
@param backlog: size of the listen queue
@param interface: the hostname to bind to, defaults to '' (all)
"""
class IReactorUNIX(Interface):
"""
UNIX socket methods.
"""
def connectUNIX(
address: str, factory: "ClientFactory", timeout: float, checkPID: bool
) -> IConnector:
"""
Connect a client protocol to a UNIX socket.
@param address: a path to a unix socket on the filesystem.
@param factory: a L{twisted.internet.protocol.ClientFactory} instance
@param timeout: number of seconds to wait before assuming the connection
has failed.
@param checkPID: if True, check for a pid file to verify that a server
is listening. If C{address} is a Linux abstract namespace path,
this must be C{False}.
@return: An object which provides L{IConnector}.
"""
def listenUNIX(
address: str, factory: "Factory", backlog: int, mode: int, wantPID: bool
) -> "IListeningPort":
"""
Listen on a UNIX socket.
@param address: a path to a unix socket on the filesystem.
@param factory: a L{twisted.internet.protocol.Factory} instance.
@param backlog: number of connections to allow in backlog.
@param mode: The mode (B{not} umask) to set on the unix socket. See
platform specific documentation for information about how this
might affect connection attempts.
@param wantPID: if True, create a pidfile for the socket. If C{address}
is a Linux abstract namespace path, this must be C{False}.
@return: An object which provides L{IListeningPort}.
"""
class IReactorUNIXDatagram(Interface):
"""
Datagram UNIX socket methods.
"""
def connectUNIXDatagram(
address: str,
protocol: "ConnectedDatagramProtocol",
maxPacketSize: int,
mode: int,
bindAddress: Optional[Tuple[str, int]],
) -> IConnector:
"""
Connect a client protocol to a datagram UNIX socket.
@param address: a path to a unix socket on the filesystem.
@param protocol: a L{twisted.internet.protocol.ConnectedDatagramProtocol} instance
@param maxPacketSize: maximum packet size to accept
@param mode: The mode (B{not} umask) to set on the unix socket. See
platform specific documentation for information about how this
might affect connection attempts.
@param bindAddress: address to bind to
@return: An object which provides L{IConnector}.
"""
def listenUNIXDatagram(
address: str, protocol: "DatagramProtocol", maxPacketSize: int, mode: int
) -> "IListeningPort":
"""
Listen on a datagram UNIX socket.
@param address: a path to a unix socket on the filesystem.
@param protocol: a L{twisted.internet.protocol.DatagramProtocol} instance.
@param maxPacketSize: maximum packet size to accept
@param mode: The mode (B{not} umask) to set on the unix socket. See
platform specific documentation for information about how this
might affect connection attempts.
@return: An object which provides L{IListeningPort}.
"""
class IReactorWin32Events(Interface):
"""
Win32 Event API methods
@since: 10.2
"""
def addEvent(event: object, fd: "FileDescriptor", action: str) -> None:
"""
Add a new win32 event to the event loop.
@param event: a Win32 event object created using win32event.CreateEvent()
@param fd: an instance of L{twisted.internet.abstract.FileDescriptor}
@param action: a string that is a method name of the fd instance.
This method is called in response to the event.
"""
def removeEvent(event: object) -> None:
"""
Remove an event.
@param event: a Win32 event object added using L{IReactorWin32Events.addEvent}
@return: None
"""
class IReactorUDP(Interface):
"""
UDP socket methods.
"""
def listenUDP(
port: int, protocol: "DatagramProtocol", interface: str, maxPacketSize: int
) -> "IListeningPort":
"""
Connects a given L{DatagramProtocol} to the given numeric UDP port.
@param port: A port number on which to listen.
@param protocol: A L{DatagramProtocol} instance which will be
connected to the given C{port}.
@param interface: The local IPv4 or IPv6 address to which to bind;
defaults to '', ie all IPv4 addresses.
@param maxPacketSize: The maximum packet size to accept.
@return: object which provides L{IListeningPort}.
"""
class IReactorMulticast(Interface):
"""
UDP socket methods that support multicast.
IMPORTANT: This is an experimental new interface. It may change
without backwards compatibility. Suggestions are welcome.
"""
def listenMulticast(
port: int,
protocol: "DatagramProtocol",
interface: str,
maxPacketSize: int,
listenMultiple: bool,
) -> "IListeningPort":
"""
Connects a given
L{DatagramProtocol<twisted.internet.protocol.DatagramProtocol>} to the
given numeric UDP port.
@param listenMultiple: If set to True, allows multiple sockets to
bind to the same address and port number at the same time.
@returns: An object which provides L{IListeningPort}.
@see: L{twisted.internet.interfaces.IMulticastTransport}
@see: U{http://twistedmatrix.com/documents/current/core/howto/udp.html}
"""
class IReactorSocket(Interface):
"""
Methods which allow a reactor to use externally created sockets.
For example, to use C{adoptStreamPort} to implement behavior equivalent
to that of L{IReactorTCP.listenTCP}, you might write code like this::
from socket import SOMAXCONN, AF_INET, SOCK_STREAM, socket
portSocket = socket(AF_INET, SOCK_STREAM)
# Set FD_CLOEXEC on port, left as an exercise. Then make it into a
# non-blocking listening port:
portSocket.setblocking(False)
portSocket.bind(('192.168.1.2', 12345))
portSocket.listen(SOMAXCONN)
# Now have the reactor use it as a TCP port
port = reactor.adoptStreamPort(
portSocket.fileno(), AF_INET, YourFactory())
# portSocket itself is no longer necessary, and needs to be cleaned
# up by us.
portSocket.close()
# Whenever the server is no longer needed, stop it as usual.
stoppedDeferred = port.stopListening()
Another potential use is to inherit a listening descriptor from a parent
process (for example, systemd or launchd), or to receive one over a UNIX
domain socket.
Some plans for extending this interface exist. See:
- U{http://twistedmatrix.com/trac/ticket/6594}: AF_UNIX SOCK_DGRAM ports
"""
def adoptStreamPort(
fileDescriptor: int, addressFamily: "AddressFamily", factory: "ServerFactory"
) -> "IListeningPort":
"""
Add an existing listening I{SOCK_STREAM} socket to the reactor to
monitor for new connections to accept and handle.
@param fileDescriptor: A file descriptor associated with a socket which
is already bound to an address and marked as listening. The socket
must be set non-blocking. Any additional flags (for example,
close-on-exec) must also be set by application code. Application
code is responsible for closing the file descriptor, which may be
done as soon as C{adoptStreamPort} returns.
@param addressFamily: The address family (or I{domain}) of the socket.
For example, L{socket.AF_INET6}.
@param factory: A L{ServerFactory} instance to use to create new
protocols to handle connections accepted via this socket.
@return: An object providing L{IListeningPort}.
@raise twisted.internet.error.UnsupportedAddressFamily: If the
given address family is not supported by this reactor, or
not supported with the given socket type.
@raise twisted.internet.error.UnsupportedSocketType: If the
given socket type is not supported by this reactor, or not
supported with the given socket type.
"""
def adoptStreamConnection(
fileDescriptor: int, addressFamily: "AddressFamily", factory: "ServerFactory"
) -> None:
"""
Add an existing connected I{SOCK_STREAM} socket to the reactor to
monitor for data.
Note that the given factory won't have its C{startFactory} and
C{stopFactory} methods called, as there is no sensible time to call
them in this situation.
@param fileDescriptor: A file descriptor associated with a socket which
is already connected. The socket must be set non-blocking. Any
additional flags (for example, close-on-exec) must also be set by
application code. Application code is responsible for closing the
file descriptor, which may be done as soon as
C{adoptStreamConnection} returns.
@param addressFamily: The address family (or I{domain}) of the socket.
For example, L{socket.AF_INET6}.
@param factory: A L{ServerFactory} instance to use to create a new
protocol to handle the connection via this socket.
@raise UnsupportedAddressFamily: If the given address family is not
supported by this reactor, or not supported with the given socket
type.
@raise UnsupportedSocketType: If the given socket type is not supported
by this reactor, or not supported with the given socket type.
"""
def adoptDatagramPort(
fileDescriptor: int,
addressFamily: "AddressFamily",
protocol: "DatagramProtocol",
maxPacketSize: int,
) -> "IListeningPort":
"""
Add an existing listening I{SOCK_DGRAM} socket to the reactor to
monitor for read and write readiness.
@param fileDescriptor: A file descriptor associated with a socket which
is already bound to an address and marked as listening. The socket
must be set non-blocking. Any additional flags (for example,
close-on-exec) must also be set by application code. Application
code is responsible for closing the file descriptor, which may be
done as soon as C{adoptDatagramPort} returns.
@param addressFamily: The address family or I{domain} of the socket.
For example, L{socket.AF_INET6}.
@param protocol: A L{DatagramProtocol} instance to connect to
a UDP transport.
@param maxPacketSize: The maximum packet size to accept.
@return: An object providing L{IListeningPort}.
@raise UnsupportedAddressFamily: If the given address family is not
supported by this reactor, or not supported with the given socket
type.
@raise UnsupportedSocketType: If the given socket type is not supported
by this reactor, or not supported with the given socket type.
"""
class IReactorProcess(Interface):
def spawnProcess(
processProtocol: "IProcessProtocol",
executable: Union[bytes, str],
args: Sequence[Union[bytes, str]],
env: Optional[Mapping[AnyStr, AnyStr]] = None,
path: Union[None, bytes, str] = None,
uid: Optional[int] = None,
gid: Optional[int] = None,
usePTY: bool = False,
childFDs: Optional[Mapping[int, Union[int, str]]] = None,
) -> "IProcessTransport":
"""
Spawn a process, with a process protocol.
Arguments given to this function that are listed as L{bytes} or
L{unicode} may be encoded or decoded depending on the platform and the
argument type given. On UNIX systems (Linux, FreeBSD, macOS) and
Python 2 on Windows, L{unicode} arguments will be encoded down to
L{bytes} using the encoding given by L{sys.getfilesystemencoding}, to be
used with the "narrow" OS APIs. On Python 3 on Windows, L{bytes}
arguments will be decoded up to L{unicode} using the encoding given by
L{sys.getfilesystemencoding} (C{utf8}) and given to Windows's native "wide" APIs.
@param processProtocol: An object which will be notified of all events
related to the created process.
@param executable: the file name to spawn - the full path should be
used.
@param args: the command line arguments to pass to the process; a
sequence of strings. The first string should be the executable's
name.
@param env: the environment variables to pass to the child process.
The resulting behavior varies between platforms. If:
- C{env} is not set:
- On POSIX: pass an empty environment.
- On Windows: pass L{os.environ}.
- C{env} is L{None}:
- On POSIX: pass L{os.environ}.
- On Windows: pass L{os.environ}.
- C{env} is a L{dict}:
- On POSIX: pass the key/value pairs in C{env} as the
complete environment.
- On Windows: update L{os.environ} with the key/value
pairs in the L{dict} before passing it. As a
consequence of U{bug #1640
<http://twistedmatrix.com/trac/ticket/1640>}, passing
keys with empty values in an effort to unset
environment variables I{won't} unset them.
@param path: the path to run the subprocess in - defaults to the
current directory.
@param uid: user ID to run the subprocess as. (Only available on POSIX
systems.)
@param gid: group ID to run the subprocess as. (Only available on
POSIX systems.)
@param usePTY: if true, run this process in a pseudo-terminal.
optionally a tuple of C{(masterfd, slavefd, ttyname)}, in which
case use those file descriptors. (Not available on all systems.)
@param childFDs: A dictionary mapping file descriptors in the new child
process to an integer or to the string 'r' or 'w'.
If the value is an integer, it specifies a file descriptor in the
parent process which will be mapped to a file descriptor (specified
by the key) in the child process. This is useful for things like
inetd and shell-like file redirection.
If it is the string 'r', a pipe will be created and attached to the
child at that file descriptor: the child will be able to write to
that file descriptor and the parent will receive read notification
via the L{IProcessProtocol.childDataReceived} callback. This is
useful for the child's stdout and stderr.
If it is the string 'w', similar setup to the previous case will
occur, with the pipe being readable by the child instead of
writeable. The parent process can write to that file descriptor
using L{IProcessTransport.writeToChild}. This is useful for the
child's stdin.
If childFDs is not passed, the default behaviour is to use a
mapping that opens the usual stdin/stdout/stderr pipes.
@see: L{twisted.internet.protocol.ProcessProtocol}
@return: An object which provides L{IProcessTransport}.
@raise OSError: Raised with errno C{EAGAIN} or C{ENOMEM} if there are
insufficient system resources to create a new process.
"""
class IReactorTime(Interface):
"""
Time methods that a Reactor should implement.
"""
def seconds() -> float:
"""
Get the current time in seconds.
@return: A number-like object of some sort.
"""
def callLater(
delay: float, callable: Callable[..., Any], *args: object, **kwargs: object
) -> "IDelayedCall":
"""
Call a function later.
@param delay: the number of seconds to wait.
@param callable: the callable object to call later.
@param args: the arguments to call it with.
@param kwargs: the keyword arguments to call it with.
@return: An object which provides L{IDelayedCall} and can be used to
cancel the scheduled call, by calling its C{cancel()} method.
It also may be rescheduled by calling its C{delay()} or
C{reset()} methods.
"""
def getDelayedCalls() -> Sequence["IDelayedCall"]:
"""
See L{twisted.internet.interfaces.IReactorTime.getDelayedCalls}
"""
class IDelayedCall(Interface):
"""
A scheduled call.
There are probably other useful methods we can add to this interface;
suggestions are welcome.
"""
def getTime() -> float:
"""
Get time when delayed call will happen.
@return: time in seconds since epoch (a float).
"""
def cancel() -> None:
"""
Cancel the scheduled call.
@raises twisted.internet.error.AlreadyCalled: if the call has already
happened.
@raises twisted.internet.error.AlreadyCancelled: if the call has already
been cancelled.
"""
def delay(secondsLater: float) -> None:
"""
Delay the scheduled call.
@param secondsLater: how many seconds from its current firing time to delay
@raises twisted.internet.error.AlreadyCalled: if the call has already
happened.
@raises twisted.internet.error.AlreadyCancelled: if the call has already
been cancelled.
"""
def reset(secondsFromNow: float) -> None:
"""
Reset the scheduled call's timer.
@param secondsFromNow: how many seconds from now it should fire,
equivalent to C{.cancel()} and then doing another
C{reactor.callLater(secondsLater, ...)}
@raises twisted.internet.error.AlreadyCalled: if the call has already
happened.
@raises twisted.internet.error.AlreadyCancelled: if the call has already
been cancelled.
"""
def active() -> bool:
"""
@return: True if this call is still active, False if it has been
called or cancelled.
"""
class IReactorFromThreads(Interface):
"""
This interface is the set of thread-safe methods which may be invoked on
the reactor from other threads.
@since: 15.4
"""
def callFromThread(
callable: Callable[..., Any], *args: object, **kwargs: object
) -> None:
"""
Cause a function to be executed by the reactor thread.
Use this method when you want to run a function in the reactor's thread
from another thread. Calling L{callFromThread} should wake up the main
thread (where L{reactor.run() <IReactorCore.run>} is executing) and run
the given callable in that thread.
If you're writing a multi-threaded application the C{callable}
may need to be thread safe, but this method doesn't require it as such.
If you want to call a function in the next mainloop iteration, but
you're in the same thread, use L{callLater} with a delay of 0.
"""
class IReactorInThreads(Interface):
"""
This interface contains the methods exposed by a reactor which will let you
run functions in another thread.
@since: 15.4
"""
def callInThread(
callable: Callable[..., Any], *args: object, **kwargs: object
) -> None:
"""
Run the given callable object in a separate thread, with the given
arguments and keyword arguments.
"""
class IReactorThreads(IReactorFromThreads, IReactorInThreads):
"""
Dispatch methods to be run in threads.
Internally, this should use a thread pool and dispatch methods to them.
"""
def getThreadPool() -> "ThreadPool":
"""
Return the threadpool used by L{IReactorInThreads.callInThread}.
Create it first if necessary.
"""
def suggestThreadPoolSize(size: int) -> None:
"""
Suggest the size of the internal threadpool used to dispatch functions
passed to L{IReactorInThreads.callInThread}.
"""
class IReactorCore(Interface):
"""
Core methods that a Reactor must implement.
"""
running = Attribute(
"A C{bool} which is C{True} from I{during startup} to "
"I{during shutdown} and C{False} the rest of the time."
)
def resolve(name: str, timeout: Sequence[int]) -> "Deferred[str]":
"""
Return a L{twisted.internet.defer.Deferred} that will resolve
a hostname.
"""
def run() -> None:
"""
Fire 'startup' System Events, move the reactor to the 'running'
state, then run the main loop until it is stopped with C{stop()} or
C{crash()}.
"""
def stop() -> None:
"""
Fire 'shutdown' System Events, which will move the reactor to the
'stopped' state and cause C{reactor.run()} to exit.
"""
def crash() -> None:
"""
Stop the main loop *immediately*, without firing any system events.
This is named as it is because this is an extremely "rude" thing to do;
it is possible to lose data and put your system in an inconsistent
state by calling this. However, it is necessary, as sometimes a system
can become wedged in a pre-shutdown call.
"""
def iterate(delay: float) -> None:
"""
Run the main loop's I/O polling function for a period of time.
This is most useful in applications where the UI is being drawn "as
fast as possible", such as games. All pending L{IDelayedCall}s will
be called.
The reactor must have been started (via the C{run()} method) prior to
any invocations of this method. It must also be stopped manually
after the last call to this method (via the C{stop()} method). This
method is not re-entrant: you must not call it recursively; in
particular, you must not call it while the reactor is running.
"""
def fireSystemEvent(eventType: str) -> None:
"""
Fire a system-wide event.
System-wide events are things like 'startup', 'shutdown', and
'persist'.
"""
def addSystemEventTrigger(
phase: str,
eventType: str,
callable: Callable[..., Any],
*args: object,
**kwargs: object,
) -> Any:
"""
Add a function to be called when a system event occurs.
Each "system event" in Twisted, such as 'startup', 'shutdown', and
'persist', has 3 phases: 'before', 'during', and 'after' (in that
order, of course). These events will be fired internally by the
Reactor.
An implementor of this interface must only implement those events
described here.
Callbacks registered for the "before" phase may return either None or a
Deferred. The "during" phase will not execute until all of the
Deferreds from the "before" phase have fired.
Once the "during" phase is running, all of the remaining triggers must
execute; their return values must be ignored.
@param phase: a time to call the event -- either the string 'before',
'after', or 'during', describing when to call it
relative to the event's execution.
@param eventType: this is a string describing the type of event.
@param callable: the object to call before shutdown.
@param args: the arguments to call it with.
@param kwargs: the keyword arguments to call it with.
@return: an ID that can be used to remove this call with
removeSystemEventTrigger.
"""
def removeSystemEventTrigger(triggerID: Any) -> None:
"""
Removes a trigger added with addSystemEventTrigger.
@param triggerID: a value returned from addSystemEventTrigger.
@raise KeyError: If there is no system event trigger for the given
C{triggerID}.
@raise ValueError: If there is no system event trigger for the given
C{triggerID}.
@raise TypeError: If there is no system event trigger for the given
C{triggerID}.
"""
def callWhenRunning(
callable: Callable[..., Any], *args: object, **kwargs: object
) -> Optional[Any]:
"""
Call a function when the reactor is running.
If the reactor has not started, the callable will be scheduled
to run when it does start. Otherwise, the callable will be invoked
immediately.
@param callable: the callable object to call later.
@param args: the arguments to call it with.
@param kwargs: the keyword arguments to call it with.
@return: None if the callable was invoked, otherwise a system
event id for the scheduled call.
"""
class IReactorPluggableResolver(Interface):
"""
An L{IReactorPluggableResolver} is a reactor which can be customized with
an L{IResolverSimple}. This is a fairly limited interface, that supports
only IPv4; you should use L{IReactorPluggableNameResolver} instead.
@see: L{IReactorPluggableNameResolver}
"""
def installResolver(resolver: IResolverSimple) -> IResolverSimple:
"""
Set the internal resolver to use to for name lookups.
@param resolver: The new resolver to use.
@return: The previously installed resolver.
"""
class IReactorPluggableNameResolver(Interface):
"""
An L{IReactorPluggableNameResolver} is a reactor whose name resolver can be
set to a user-supplied object.
"""
nameResolver = Attribute(
"""
Read-only attribute; the resolver installed with L{installResolver}.
An L{IHostnameResolver}.
"""
)
def installNameResolver(resolver: IHostnameResolver) -> IHostnameResolver:
"""
Set the internal resolver to use for name lookups.
@param resolver: The new resolver to use.
@return: The previously installed resolver.
"""
class IReactorDaemonize(Interface):
"""
A reactor which provides hooks that need to be called before and after
daemonization.
Notes:
- This interface SHOULD NOT be called by applications.
- This interface should only be implemented by reactors as a workaround
(in particular, it's implemented currently only by kqueue()).
For details please see the comments on ticket #1918.
"""
def beforeDaemonize() -> None:
"""
Hook to be called immediately before daemonization. No reactor methods
may be called until L{afterDaemonize} is called.
"""
def afterDaemonize() -> None:
"""
Hook to be called immediately after daemonization. This may only be
called after L{beforeDaemonize} had been called previously.
"""
class IReactorFDSet(Interface):
"""
Implement me to be able to use L{IFileDescriptor} type resources.
This assumes that your main-loop uses UNIX-style numeric file descriptors
(or at least similarly opaque IDs returned from a .fileno() method)
"""
def addReader(reader: "IReadDescriptor") -> None:
"""
I add reader to the set of file descriptors to get read events for.
@param reader: An L{IReadDescriptor} provider that will be checked for
read events until it is removed from the reactor with
L{removeReader}.
"""
def addWriter(writer: "IWriteDescriptor") -> None:
"""
I add writer to the set of file descriptors to get write events for.
@param writer: An L{IWriteDescriptor} provider that will be checked for
write events until it is removed from the reactor with
L{removeWriter}.
"""
def removeReader(reader: "IReadDescriptor") -> None:
"""
Removes an object previously added with L{addReader}.
"""
def removeWriter(writer: "IWriteDescriptor") -> None:
"""
Removes an object previously added with L{addWriter}.
"""
def removeAll() -> List[Union["IReadDescriptor", "IWriteDescriptor"]]:
"""
Remove all readers and writers.
Should not remove reactor internal reactor connections (like a waker).
@return: A list of L{IReadDescriptor} and L{IWriteDescriptor} providers
which were removed.
"""
def getReaders() -> List["IReadDescriptor"]:
"""
Return the list of file descriptors currently monitored for input
events by the reactor.
@return: the list of file descriptors monitored for input events.
"""
def getWriters() -> List["IWriteDescriptor"]:
"""
Return the list file descriptors currently monitored for output events
by the reactor.
@return: the list of file descriptors monitored for output events.
"""
class IListeningPort(Interface):
"""
A listening port.
"""
def startListening() -> None:
"""
Start listening on this port.
@raise CannotListenError: If it cannot listen on this port (e.g., it is
a TCP port and it cannot bind to the required
port number).
"""
def stopListening() -> Optional["Deferred[None]"]:
"""
Stop listening on this port.
If it does not complete immediately, will return Deferred that fires
upon completion.
"""
def getHost() -> IAddress:
"""
Get the host that this port is listening for.
@return: An L{IAddress} provider.
"""
class ILoggingContext(Interface):
"""
Give context information that will be used to log events generated by
this item.
"""
def logPrefix() -> str:
"""
@return: Prefix used during log formatting to indicate context.
"""
class IFileDescriptor(ILoggingContext):
"""
An interface representing a UNIX-style numeric file descriptor.
"""
def fileno() -> object:
"""
@return: The platform-specified representation of a file descriptor
number. Or C{-1} if the descriptor no longer has a valid file
descriptor number associated with it. As long as the descriptor
is valid, calls to this method on a particular instance must
return the same value.
"""
def connectionLost(reason: Failure) -> None:
"""
Called when the connection was lost.
This is called when the connection on a selectable object has been
lost. It will be called whether the connection was closed explicitly,
an exception occurred in an event handler, or the other end of the
connection closed it first.
See also L{IHalfCloseableDescriptor} if your descriptor wants to be
notified separately of the two halves of the connection being closed.
@param reason: A failure instance indicating the reason why the
connection was lost. L{error.ConnectionLost} and
L{error.ConnectionDone} are of special note, but the
failure may be of other classes as well.
"""
class IReadDescriptor(IFileDescriptor):
"""
An L{IFileDescriptor} that can read.
This interface is generally used in conjunction with L{IReactorFDSet}.
"""
def doRead() -> Optional[Failure]:
"""
Some data is available for reading on your descriptor.
@return: If an error is encountered which causes the descriptor to
no longer be valid, a L{Failure} should be returned. Otherwise,
L{None}.
"""
class IWriteDescriptor(IFileDescriptor):
"""
An L{IFileDescriptor} that can write.
This interface is generally used in conjunction with L{IReactorFDSet}.
"""
def doWrite() -> Optional[Failure]:
"""
Some data can be written to your descriptor.
@return: If an error is encountered which causes the descriptor to
no longer be valid, a L{Failure} should be returned. Otherwise,
L{None}.
"""
class IReadWriteDescriptor(IReadDescriptor, IWriteDescriptor):
"""
An L{IFileDescriptor} that can both read and write.
"""
class IHalfCloseableDescriptor(Interface):
"""
A descriptor that can be half-closed.
"""
def writeConnectionLost(reason: Failure) -> None:
"""
Indicates write connection was lost.
"""
def readConnectionLost(reason: Failure) -> None:
"""
Indicates read connection was lost.
"""
class ISystemHandle(Interface):
"""
An object that wraps a networking OS-specific handle.
"""
def getHandle() -> object:
"""
Return a system- and reactor-specific handle.
This might be a socket.socket() object, or some other type of
object, depending on which reactor is being used. Use and
manipulate at your own risk.
This might be used in cases where you want to set specific
options not exposed by the Twisted APIs.
"""
class IConsumer(Interface):
"""
A consumer consumes data from a producer.
"""
def registerProducer(producer: "IProducer", streaming: bool) -> None:
"""
Register to receive data from a producer.
This sets self to be a consumer for a producer. When this object runs
out of data (as when a send(2) call on a socket succeeds in moving the
last data from a userspace buffer into a kernelspace buffer), it will
ask the producer to resumeProducing().
For L{IPullProducer} providers, C{resumeProducing} will be called once
each time data is required.
For L{IPushProducer} providers, C{pauseProducing} will be called
whenever the write buffer fills up and C{resumeProducing} will only be
called when it empties. The consumer will only call C{resumeProducing}
to balance a previous C{pauseProducing} call; the producer is assumed
to start in an un-paused state.
@param streaming: C{True} if C{producer} provides L{IPushProducer},
C{False} if C{producer} provides L{IPullProducer}.
@raise RuntimeError: If a producer is already registered.
"""
def unregisterProducer() -> None:
"""
Stop consuming data from a producer, without disconnecting.
"""
def write(data: bytes) -> None:
"""
The producer will write data by calling this method.
The implementation must be non-blocking and perform whatever
buffering is necessary. If the producer has provided enough data
for now and it is a L{IPushProducer}, the consumer may call its
C{pauseProducing} method.
"""
class IProducer(Interface):
"""
A producer produces data for a consumer.
Typically producing is done by calling the C{write} method of a class
implementing L{IConsumer}.
"""
def stopProducing() -> None:
"""
Stop producing data.
This tells a producer that its consumer has died, so it must stop
producing data for good.
"""
class IPushProducer(IProducer):
"""
A push producer, also known as a streaming producer is expected to
produce (write to this consumer) data on a continuous basis, unless
it has been paused. A paused push producer will resume producing
after its C{resumeProducing()} method is called. For a push producer
which is not pauseable, these functions may be noops.
"""
def pauseProducing() -> None:
"""
Pause producing data.
Tells a producer that it has produced too much data to process for
the time being, and to stop until C{resumeProducing()} is called.
"""
def resumeProducing() -> None:
"""
Resume producing data.
This tells a producer to re-add itself to the main loop and produce
more data for its consumer.
"""
class IPullProducer(IProducer):
"""
A pull producer, also known as a non-streaming producer, is
expected to produce data each time L{resumeProducing()} is called.
"""
def resumeProducing() -> None:
"""
Produce data for the consumer a single time.
This tells a producer to produce data for the consumer once
(not repeatedly, once only). Typically this will be done
by calling the consumer's C{write} method a single time with
produced data. The producer should produce data before returning
from C{resumeProducing()}, that is, it should not schedule a deferred
write.
"""
class IProtocol(Interface):
def dataReceived(data: bytes) -> None:
"""
Called whenever data is received.
Use this method to translate to a higher-level message. Usually, some
callback will be made upon the receipt of each complete protocol
message.
Please keep in mind that you will probably need to buffer some data
as partial (or multiple) protocol messages may be received! We
recommend that unit tests for protocols call through to this method
with differing chunk sizes, down to one byte at a time.
@param data: bytes of indeterminate length
"""
def connectionLost(reason: Failure) -> None:
"""
Called when the connection is shut down.
Clear any circular references here, and any external references
to this Protocol. The connection has been closed. The C{reason}
Failure wraps a L{twisted.internet.error.ConnectionDone} or
L{twisted.internet.error.ConnectionLost} instance (or a subclass
of one of those).
"""
def makeConnection(transport: "ITransport") -> None:
"""
Make a connection to a transport and a server.
"""
def connectionMade() -> None:
"""
Called when a connection is made.
This may be considered the initializer of the protocol, because
it is called when the connection is completed. For clients,
this is called once the connection to the server has been
established; for servers, this is called after an accept() call
stops blocking and a socket has been received. If you need to
send any greeting or initial message, do it here.
"""
class IProcessProtocol(Interface):
"""
Interface for process-related event handlers.
"""
def makeConnection(process: "IProcessTransport") -> None:
"""
Called when the process has been created.
@param process: An object representing the process which has been
created and associated with this protocol.
"""
def childDataReceived(childFD: int, data: bytes) -> None:
"""
Called when data arrives from the child process.
@param childFD: The file descriptor from which the data was
received.
@param data: The data read from the child's file descriptor.
"""
def childConnectionLost(childFD: int) -> None:
"""
Called when a file descriptor associated with the child process is
closed.
@param childFD: The file descriptor which was closed.
"""
def processExited(reason: Failure) -> None:
"""
Called when the child process exits.
@param reason: A failure giving the reason the child process
terminated. The type of exception for this failure is either
L{twisted.internet.error.ProcessDone} or
L{twisted.internet.error.ProcessTerminated}.
@since: 8.2
"""
def processEnded(reason: Failure) -> None:
"""
Called when the child process exits and all file descriptors associated
with it have been closed.
@param reason: A failure giving the reason the child process
terminated. The type of exception for this failure is either
L{twisted.internet.error.ProcessDone} or
L{twisted.internet.error.ProcessTerminated}.
"""
class IHalfCloseableProtocol(Interface):
"""
Implemented to indicate they want notification of half-closes.
TCP supports the notion of half-closing the connection, e.g.
closing the write side but still not stopping reading. A protocol
that implements this interface will be notified of such events,
instead of having connectionLost called.
"""
def readConnectionLost() -> None:
"""
Notification of the read connection being closed.
This indicates peer did half-close of write side. It is now
the responsibility of the this protocol to call
loseConnection(). In addition, the protocol MUST make sure a
reference to it still exists (i.e. by doing a callLater with
one of its methods, etc.) as the reactor will only have a
reference to it if it is writing.
If the protocol does not do so, it might get garbage collected
without the connectionLost method ever being called.
"""
def writeConnectionLost() -> None:
"""
Notification of the write connection being closed.
This will never be called for TCP connections as TCP does not
support notification of this type of half-close.
"""
class IHandshakeListener(Interface):
"""
An interface implemented by a L{IProtocol} to indicate that it would like
to be notified when TLS handshakes complete when run over a TLS-based
transport.
This interface is only guaranteed to be called when run over a TLS-based
transport: non TLS-based transports will not respect this interface.
"""
def handshakeCompleted() -> None:
"""
Notification of the TLS handshake being completed.
This notification fires when OpenSSL has completed the TLS handshake.
At this point the TLS connection is established, and the protocol can
interrogate its transport (usually an L{ISSLTransport}) for details of
the TLS connection.
This notification *also* fires whenever the TLS session is
renegotiated. As a result, protocols that have certain minimum security
requirements should implement this interface to ensure that they are
able to re-evaluate the security of the TLS session if it changes.
"""
class IFileDescriptorReceiver(Interface):
"""
Protocols may implement L{IFileDescriptorReceiver} to receive file
descriptors sent to them. This is useful in conjunction with
L{IUNIXTransport}, which allows file descriptors to be sent between
processes on a single host.
"""
def fileDescriptorReceived(descriptor: int) -> None:
"""
Called when a file descriptor is received over the connection.
@param descriptor: The descriptor which was received.
@return: L{None}
"""
class IProtocolFactory(Interface):
"""
Interface for protocol factories.
"""
def buildProtocol(addr: IAddress) -> Optional[IProtocol]:
"""
Called when a connection has been established to addr.
If None is returned, the connection is assumed to have been refused,
and the Port will close the connection.
@param addr: The address of the newly-established connection
@return: None if the connection was refused, otherwise an object
providing L{IProtocol}.
"""
def doStart() -> None:
"""
Called every time this is connected to a Port or Connector.
"""
def doStop() -> None:
"""
Called every time this is unconnected from a Port or Connector.
"""
class ITransport(Interface):
"""
I am a transport for bytes.
I represent (and wrap) the physical connection and synchronicity
of the framework which is talking to the network. I make no
representations about whether calls to me will happen immediately
or require returning to a control loop, or whether they will happen
in the same or another thread. Consider methods of this class
(aside from getPeer) to be 'thrown over the wall', to happen at some
indeterminate time.
"""
def write(data: bytes) -> None:
"""
Write some data to the physical connection, in sequence, in a
non-blocking fashion.
If possible, make sure that it is all written. No data will
ever be lost, although (obviously) the connection may be closed
before it all gets through.
@param data: The data to write.
"""
def writeSequence(data: Iterable[bytes]) -> None:
"""
Write an iterable of byte strings to the physical connection.
If possible, make sure that all of the data is written to
the socket at once, without first copying it all into a
single byte string.
@param data: The data to write.
"""
def loseConnection() -> None:
"""
Close my connection, after writing all pending data.
Note that if there is a registered producer on a transport it
will not be closed until the producer has been unregistered.
"""
def getPeer() -> IAddress:
"""
Get the remote address of this connection.
Treat this method with caution. It is the unfortunate result of the
CGI and Jabber standards, but should not be considered reliable for
the usual host of reasons; port forwarding, proxying, firewalls, IP
masquerading, etc.
@return: An L{IAddress} provider.
"""
def getHost() -> IAddress:
"""
Similar to getPeer, but returns an address describing this side of the
connection.
@return: An L{IAddress} provider.
"""
class ITCPTransport(ITransport):
"""
A TCP based transport.
"""
def loseWriteConnection() -> None:
"""
Half-close the write side of a TCP connection.
If the protocol instance this is attached to provides
IHalfCloseableProtocol, it will get notified when the operation is
done. When closing write connection, as with loseConnection this will
only happen when buffer has emptied and there is no registered
producer.
"""
def abortConnection() -> None:
"""
Close the connection abruptly.
Discards any buffered data, stops any registered producer,
and, if possible, notifies the other end of the unclean
closure.
@since: 11.1
"""
def getTcpNoDelay() -> bool:
"""
Return if C{TCP_NODELAY} is enabled.
"""
def setTcpNoDelay(enabled: bool) -> None:
"""
Enable/disable C{TCP_NODELAY}.
Enabling C{TCP_NODELAY} turns off Nagle's algorithm. Small packets are
sent sooner, possibly at the expense of overall throughput.
"""
def getTcpKeepAlive() -> bool:
"""
Return if C{SO_KEEPALIVE} is enabled.
"""
def setTcpKeepAlive(enabled: bool) -> None:
"""
Enable/disable C{SO_KEEPALIVE}.
Enabling C{SO_KEEPALIVE} sends packets periodically when the connection
is otherwise idle, usually once every two hours. They are intended
to allow detection of lost peers in a non-infinite amount of time.
"""
def getHost() -> Union["IPv4Address", "IPv6Address"]:
"""
Returns L{IPv4Address} or L{IPv6Address}.
"""
def getPeer() -> Union["IPv4Address", "IPv6Address"]:
"""
Returns L{IPv4Address} or L{IPv6Address}.
"""
class IUNIXTransport(ITransport):
"""
Transport for stream-oriented unix domain connections.
"""
def sendFileDescriptor(descriptor: int) -> None:
"""
Send a duplicate of this (file, socket, pipe, etc) descriptor to the
other end of this connection.
The send is non-blocking and will be queued if it cannot be performed
immediately. The send will be processed in order with respect to other
C{sendFileDescriptor} calls on this transport, but not necessarily with
respect to C{write} calls on this transport. The send can only be
processed if there are also bytes in the normal connection-oriented send
buffer (ie, you must call C{write} at least as many times as you call
C{sendFileDescriptor}).
@param descriptor: An C{int} giving a valid file descriptor in this
process. Note that a I{file descriptor} may actually refer to a
socket, a pipe, or anything else POSIX tries to treat in the same
way as a file.
"""
class IOpenSSLServerConnectionCreator(Interface):
"""
A provider of L{IOpenSSLServerConnectionCreator} can create
L{OpenSSL.SSL.Connection} objects for TLS servers.
@see: L{twisted.internet.ssl}
@note: Creating OpenSSL connection objects is subtle, error-prone, and
security-critical. Before implementing this interface yourself,
consider using L{twisted.internet.ssl.CertificateOptions} as your
C{contextFactory}. (For historical reasons, that class does not
actually I{implement} this interface; nevertheless it is usable in all
Twisted APIs which require a provider of this interface.)
"""
def serverConnectionForTLS(
tlsProtocol: "TLSMemoryBIOProtocol",
) -> "OpenSSLConnection":
"""
Create a connection for the given server protocol.
@return: an OpenSSL connection object configured appropriately for the
given Twisted protocol.
"""
class IOpenSSLClientConnectionCreator(Interface):
"""
A provider of L{IOpenSSLClientConnectionCreator} can create
L{OpenSSL.SSL.Connection} objects for TLS clients.
@see: L{twisted.internet.ssl}
@note: Creating OpenSSL connection objects is subtle, error-prone, and
security-critical. Before implementing this interface yourself,
consider using L{twisted.internet.ssl.optionsForClientTLS} as your
C{contextFactory}.
"""
def clientConnectionForTLS(
tlsProtocol: "TLSMemoryBIOProtocol",
) -> "OpenSSLConnection":
"""
Create a connection for the given client protocol.
@param tlsProtocol: the client protocol making the request.
@return: an OpenSSL connection object configured appropriately for the
given Twisted protocol.
"""
class IProtocolNegotiationFactory(Interface):
"""
A provider of L{IProtocolNegotiationFactory} can provide information about
the various protocols that the factory can create implementations of. This
can be used, for example, to provide protocol names for Next Protocol
Negotiation and Application Layer Protocol Negotiation.
@see: L{twisted.internet.ssl}
"""
def acceptableProtocols() -> List[bytes]:
"""
Returns a list of protocols that can be spoken by the connection
factory in the form of ALPN tokens, as laid out in the IANA registry
for ALPN tokens.
@return: a list of ALPN tokens in order of preference.
"""
class IOpenSSLContextFactory(Interface):
"""
A provider of L{IOpenSSLContextFactory} is capable of generating
L{OpenSSL.SSL.Context} classes suitable for configuring TLS on a
connection. A provider will store enough state to be able to generate these
contexts as needed for individual connections.
@see: L{twisted.internet.ssl}
"""
def getContext() -> "OpenSSLContext":
"""
Returns a TLS context object, suitable for securing a TLS connection.
This context object will be appropriately customized for the connection
based on the state in this object.
@return: A TLS context object.
"""
class ITLSTransport(ITCPTransport):
"""
A TCP transport that supports switching to TLS midstream.
Once TLS mode is started the transport will implement L{ISSLTransport}.
"""
def startTLS(
contextFactory: Union[
IOpenSSLClientConnectionCreator, IOpenSSLServerConnectionCreator
]
) -> None:
"""
Initiate TLS negotiation.
@param contextFactory: An object which creates appropriately configured
TLS connections.
For clients, use L{twisted.internet.ssl.optionsForClientTLS}; for
servers, use L{twisted.internet.ssl.CertificateOptions}.
@type contextFactory: L{IOpenSSLClientConnectionCreator} or
L{IOpenSSLServerConnectionCreator}, depending on whether this
L{ITLSTransport} is a server or not. If the appropriate interface
is not provided by the value given for C{contextFactory}, it must
be an implementor of L{IOpenSSLContextFactory}.
"""
class ISSLTransport(ITCPTransport):
"""
A SSL/TLS based transport.
"""
def getPeerCertificate() -> object:
"""
Return an object with the peer's certificate info.
"""
class INegotiated(ISSLTransport):
"""
A TLS based transport that supports using ALPN/NPN to negotiate the
protocol to be used inside the encrypted tunnel.
"""
negotiatedProtocol = Attribute(
"""
The protocol selected to be spoken using ALPN/NPN. The result from ALPN
is preferred to the result from NPN if both were used. If the remote
peer does not support ALPN or NPN, or neither NPN or ALPN are available
on this machine, will be L{None}. Otherwise, will be the name of the
selected protocol as C{bytes}. Note that until the handshake has
completed this property may incorrectly return L{None}: wait until data
has been received before trusting it (see
https://twistedmatrix.com/trac/ticket/6024).
"""
)
class ICipher(Interface):
"""
A TLS cipher.
"""
fullName = Attribute("The fully qualified name of the cipher in L{unicode}.")
class IAcceptableCiphers(Interface):
"""
A list of acceptable ciphers for a TLS context.
"""
def selectCiphers(availableCiphers: Tuple[ICipher]) -> Tuple[ICipher]:
"""
Choose which ciphers to allow to be negotiated on a TLS connection.
@param availableCiphers: A L{tuple} of L{ICipher} which gives the names
of all ciphers supported by the TLS implementation in use.
@return: A L{tuple} of L{ICipher} which represents the ciphers
which may be negotiated on the TLS connection. The result is
ordered by preference with more preferred ciphers appearing
earlier.
"""
class IProcessTransport(ITransport):
"""
A process transport.
"""
pid = Attribute(
"From before L{IProcessProtocol.makeConnection} is called to before "
"L{IProcessProtocol.processEnded} is called, C{pid} is an L{int} "
"giving the platform process ID of this process. C{pid} is L{None} "
"at all other times."
)
def closeStdin() -> None:
"""
Close stdin after all data has been written out.
"""
def closeStdout() -> None:
"""
Close stdout.
"""
def closeStderr() -> None:
"""
Close stderr.
"""
def closeChildFD(descriptor: int) -> None:
"""
Close a file descriptor which is connected to the child process, identified
by its FD in the child process.
"""
def writeToChild(childFD: int, data: bytes) -> None:
"""
Similar to L{ITransport.write} but also allows the file descriptor in
the child process which will receive the bytes to be specified.
@param childFD: The file descriptor to which to write.
@param data: The bytes to write.
@raise KeyError: If C{childFD} is not a file descriptor that was mapped
in the child when L{IReactorProcess.spawnProcess} was used to create
it.
"""
def loseConnection() -> None:
"""
Close stdin, stderr and stdout.
"""
def signalProcess(signalID: Union[str, int]) -> None:
"""
Send a signal to the process.
@param signalID: can be
- one of C{"KILL"}, C{"TERM"}, or C{"INT"}.
These will be implemented in a
cross-platform manner, and so should be used
if possible.
- an integer, where it represents a POSIX
signal ID.
@raise twisted.internet.error.ProcessExitedAlready: If the process has
already exited.
@raise OSError: If the C{os.kill} call fails with an errno different
from C{ESRCH}.
"""
class IServiceCollection(Interface):
"""
An object which provides access to a collection of services.
"""
def getServiceNamed(serviceName: str) -> object:
"""
Retrieve the named service from this application.
Raise a C{KeyError} if there is no such service name.
"""
def addService(service: object) -> None:
"""
Add a service to this collection.
"""
def removeService(service: object) -> None:
"""
Remove a service from this collection.
"""
class IUDPTransport(Interface):
"""
Transport for UDP DatagramProtocols.
"""
def write(packet: bytes, addr: Optional[Tuple[str, int]]) -> None:
"""
Write packet to given address.
@param addr: a tuple of (ip, port). For connected transports must
be the address the transport is connected to, or None.
In non-connected mode this is mandatory.
@raise twisted.internet.error.MessageLengthError: C{packet} was too
long.
"""
def connect(host: str, port: int) -> None:
"""
Connect the transport to an address.
This changes it to connected mode. Datagrams can only be sent to
this address, and will only be received from this address. In addition
the protocol's connectionRefused method might get called if destination
is not receiving datagrams.
@param host: an IP address, not a domain name ('127.0.0.1', not 'localhost')
@param port: port to connect to.
"""
def getHost() -> Union["IPv4Address", "IPv6Address"]:
"""
Get this port's host address.
@return: an address describing the listening port.
"""
def stopListening() -> Optional["Deferred[None]"]:
"""
Stop listening on this port.
If it does not complete immediately, will return L{Deferred} that fires
upon completion.
"""
def setBroadcastAllowed(enabled: bool) -> None:
"""
Set whether this port may broadcast.
@param enabled: Whether the port may broadcast.
"""
def getBroadcastAllowed() -> bool:
"""
Checks if broadcast is currently allowed on this port.
@return: Whether this port may broadcast.
"""
class IUNIXDatagramTransport(Interface):
"""
Transport for UDP PacketProtocols.
"""
def write(packet: bytes, addr: str) -> None:
"""
Write packet to given address.
"""
def getHost() -> "UNIXAddress":
"""
Returns L{UNIXAddress}.
"""
class IUNIXDatagramConnectedTransport(Interface):
"""
Transport for UDP ConnectedPacketProtocols.
"""
def write(packet: bytes) -> None:
"""
Write packet to address we are connected to.
"""
def getHost() -> "UNIXAddress":
"""
Returns L{UNIXAddress}.
"""
def getPeer() -> "UNIXAddress":
"""
Returns L{UNIXAddress}.
"""
class IMulticastTransport(Interface):
"""
Additional functionality for multicast UDP.
"""
def getOutgoingInterface() -> str:
"""
Return interface of outgoing multicast packets.
"""
def setOutgoingInterface(addr: str) -> None:
"""
Set interface for outgoing multicast packets.
Returns Deferred of success.
"""
def getLoopbackMode() -> bool:
"""
Return if loopback mode is enabled.
"""
def setLoopbackMode(mode: bool) -> None:
"""
Set if loopback mode is enabled.
"""
def getTTL() -> int:
"""
Get time to live for multicast packets.
"""
def setTTL(ttl: int) -> None:
"""
Set time to live on multicast packets.
"""
def joinGroup(addr: str, interface: str) -> "Deferred[None]":
"""
Join a multicast group. Returns L{Deferred} of success or failure.
If an error occurs, the returned L{Deferred} will fail with
L{error.MulticastJoinError}.
"""
def leaveGroup(addr: str, interface: str) -> "Deferred[None]":
"""
Leave multicast group, return L{Deferred} of success.
"""
class IStreamClientEndpoint(Interface):
"""
A stream client endpoint is a place that L{ClientFactory} can connect to.
For example, a remote TCP host/port pair would be a TCP client endpoint.
@since: 10.1
"""
def connect(protocolFactory: IProtocolFactory) -> "Deferred[IProtocol]":
"""
Connect the C{protocolFactory} to the location specified by this
L{IStreamClientEndpoint} provider.
@param protocolFactory: A provider of L{IProtocolFactory}
@return: A L{Deferred} that results in an L{IProtocol} upon successful
connection otherwise a L{Failure} wrapping L{ConnectError} or
L{NoProtocol <twisted.internet.error.NoProtocol>}.
"""
class IStreamServerEndpoint(Interface):
"""
A stream server endpoint is a place that a L{Factory} can listen for
incoming connections.
@since: 10.1
"""
def listen(protocolFactory: IProtocolFactory) -> "Deferred[IListeningPort]":
"""
Listen with C{protocolFactory} at the location specified by this
L{IStreamServerEndpoint} provider.
@param protocolFactory: A provider of L{IProtocolFactory}
@return: A L{Deferred} that results in an L{IListeningPort} or an
L{CannotListenError}
"""
class IStreamServerEndpointStringParser(Interface):
"""
An L{IStreamServerEndpointStringParser} is like an
L{IStreamClientEndpointStringParserWithReactor}, except for
L{IStreamServerEndpoint}s instead of clients. It integrates with
L{endpoints.serverFromString} in much the same way.
"""
prefix = Attribute(
"""
A C{str}, the description prefix to respond to. For example, an
L{IStreamServerEndpointStringParser} plugin which had C{"foo"} for its
C{prefix} attribute would be called for endpoint descriptions like
C{"foo:bar:baz"} or C{"foo:"}.
"""
)
def parseStreamServer(
reactor: IReactorCore, *args: object, **kwargs: object
) -> IStreamServerEndpoint:
"""
Parse a stream server endpoint from a reactor and string-only arguments
and keyword arguments.
@see: L{IStreamClientEndpointStringParserWithReactor.parseStreamClient}
@return: a stream server endpoint
"""
class IStreamClientEndpointStringParserWithReactor(Interface):
"""
An L{IStreamClientEndpointStringParserWithReactor} is a parser which can
convert a set of string C{*args} and C{**kwargs} into an
L{IStreamClientEndpoint} provider.
This interface is really only useful in the context of the plugin system
for L{endpoints.clientFromString}. See the document entitled "I{The
Twisted Plugin System}" for more details on how to write a plugin.
If you place an L{IStreamClientEndpointStringParserWithReactor} plugin in
the C{twisted.plugins} package, that plugin's C{parseStreamClient} method
will be used to produce endpoints for any description string that begins
with the result of that L{IStreamClientEndpointStringParserWithReactor}'s
prefix attribute.
"""
prefix = Attribute(
"""
L{bytes}, the description prefix to respond to. For example, an
L{IStreamClientEndpointStringParserWithReactor} plugin which had
C{b"foo"} for its C{prefix} attribute would be called for endpoint
descriptions like C{b"foo:bar:baz"} or C{b"foo:"}.
"""
)
def parseStreamClient(
reactor: IReactorCore, *args: object, **kwargs: object
) -> IStreamClientEndpoint:
"""
This method is invoked by L{endpoints.clientFromString}, if the type of
endpoint matches the return value from this
L{IStreamClientEndpointStringParserWithReactor}'s C{prefix} method.
@param reactor: The reactor passed to L{endpoints.clientFromString}.
@param args: The byte string arguments, minus the endpoint type, in the
endpoint description string, parsed according to the rules
described in L{endpoints.quoteStringArgument}. For example, if the
description were C{b"my-type:foo:bar:baz=qux"}, C{args} would be
C{(b'foo', b'bar')}
@param kwargs: The byte string arguments from the endpoint description
passed as keyword arguments. For example, if the description were
C{b"my-type:foo:bar:baz=qux"}, C{kwargs} would be
C{dict(baz=b'qux')}.
@return: a client endpoint
"""
class _ISupportsExitSignalCapturing(Interface):
"""
An implementor of L{_ISupportsExitSignalCapturing} will capture the
value of any delivered exit signal (SIGINT, SIGTERM, SIGBREAK) for which
it has installed a handler. The caught signal number is made available in
the _exitSignal attribute.
"""
_exitSignal = Attribute(
"""
C{int} or C{None}, the integer exit signal delivered to the
application, or None if no signal was delivered.
"""
)
|