Try to create a socket and, if it's not a datagram socket, connect it to the other end. This method is called during handler initialization, but it's not regarded as an error if the other end isn't listening yet --- the method will be called again when emitting an ev
(self)
| 905 | raise |
| 906 | |
| 907 | def createSocket(self): |
| 908 | """ |
| 909 | Try to create a socket and, if it's not a datagram socket, connect it |
| 910 | to the other end. This method is called during handler initialization, |
| 911 | but it's not regarded as an error if the other end isn't listening yet |
| 912 | --- the method will be called again when emitting an event, |
| 913 | if there is no socket at that point. |
| 914 | """ |
| 915 | address = self.address |
| 916 | socktype = self.socktype |
| 917 | |
| 918 | if isinstance(address, str): |
| 919 | self.unixsocket = True |
| 920 | # Syslog server may be unavailable during handler initialisation. |
| 921 | # C's openlog() function also ignores connection errors. |
| 922 | # Moreover, we ignore these errors while logging, so it's not worse |
| 923 | # to ignore it also here. |
| 924 | try: |
| 925 | self._connect_unixsocket(address) |
| 926 | except OSError: |
| 927 | pass |
| 928 | else: |
| 929 | self.unixsocket = False |
| 930 | if socktype is None: |
| 931 | socktype = socket.SOCK_DGRAM |
| 932 | host, port = address |
| 933 | ress = socket.getaddrinfo(host, port, 0, socktype) |
| 934 | if not ress: |
| 935 | raise OSError("getaddrinfo returns an empty list") |
| 936 | for res in ress: |
| 937 | af, socktype, proto, _, sa = res |
| 938 | err = sock = None |
| 939 | try: |
| 940 | sock = socket.socket(af, socktype, proto) |
| 941 | if self.timeout: |
| 942 | sock.settimeout(self.timeout) |
| 943 | if socktype == socket.SOCK_STREAM: |
| 944 | sock.connect(sa) |
| 945 | break |
| 946 | except OSError as exc: |
| 947 | err = exc |
| 948 | if sock is not None: |
| 949 | sock.close() |
| 950 | if err is not None: |
| 951 | raise err |
| 952 | self.socket = sock |
| 953 | self.socktype = socktype |
| 954 | |
| 955 | def encodePriority(self, facility, priority): |
| 956 | """ |
no test coverage detected