-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatagram.py
More file actions
66 lines (57 loc) · 2.16 KB
/
datagram.py
File metadata and controls
66 lines (57 loc) · 2.16 KB
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
#!/usr/bin/env python
##
## This file is part of the OpenSIPS Python Package
## (see https://github.com/OpenSIPS/python-opensips).
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
""" Implements Datagram Connection """
import socket
from .generic_socket import GenericSocket
class Datagram(GenericSocket):
""" Datagram implementation of a socket """
def __init__(self, **kwargs):
self.ip = None
self.port = None
self.sock = None
if "unix_path" in kwargs:
self.sock_name = kwargs["unix_path"]
else:
self.ip = kwargs.get("ip", "0.0.0.0")
self.port = int(kwargs.get("port", 0))
def create(self):
if self.ip is not None:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((self.ip, self.port))
self.ip, self.port = self.sock.getsockname()
if self.ip == "0.0.0.0":
hostname = socket.gethostname()
self.ip = socket.gethostbyname(hostname)
self.sock_name = f"udp:{self.ip}:{self.port}"
else:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
self.sock.bind(self.sock_name)
# we are waiting blocking, so we don't have to change this
self.sock.settimeout(0.1)
return self.sock_name
def read(self):
try:
return self.sock.recv(65535)
except socket.timeout:
return None
def destroy(self):
if not self.sock:
return
self.sock.close()
self.sock = None