-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_socket.py
More file actions
28 lines (23 loc) · 748 Bytes
/
open_socket.py
File metadata and controls
28 lines (23 loc) · 748 Bytes
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
import logging
logger = logging.getLogger(__name__)
import socket
import sys
def open_socket(port=5050):
try:
IP = socket.gethostbyname(socket.gethostname()) # This machine's ip
ADDR = (IP, port)
except:
logger.critical(f"Could not determine this machines IP address. Exiting.")
sys.exit()
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # IPV 4, TCP
sock.bind(ADDR)
sock.listen(100)
logger.info(f"sock created a listening socket: {sock}")
except socket.error as e:
logger.critical(
f"There was a problem configuring the socket, cannot proceed. Exiting. {ADDR} - {e}"
)
sys.exit()
else:
return sock