forked from twisted/twisted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.py
2756 lines (2163 loc) · 95.9 KB
/
interfaces.py
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
# 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