Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/test/selenium/webdriver/common/network.py
1865 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
# module for getting the lan ip address of the computer
19
import os
20
import socket
21
22
if os.name != "nt":
23
import fcntl
24
import struct
25
26
def get_interface_ip(ifname):
27
def _bytes(value, encoding):
28
return bytes(value, encoding)
29
30
sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
31
return socket.inet_ntoa(
32
fcntl.ioctl(sckt.fileno(), 0x8915, struct.pack("256s", _bytes(ifname[:15], "utf-8")))[20:24] # SIOCGIFADDR
33
)
34
35
36
def get_lan_ip():
37
if os.environ.get("CI") == "true":
38
return "0.0.0.0"
39
40
try:
41
ip = socket.gethostbyname(socket.gethostname())
42
except Exception:
43
return "0.0.0.0"
44
if ip.startswith("127.") and os.name != "nt":
45
interfaces = [
46
"eth0",
47
"eth1",
48
"eth2",
49
"en0",
50
"en1",
51
"en2",
52
"en3",
53
"en4",
54
"eno0",
55
"eno1",
56
"eno2",
57
"eno3",
58
"eno4",
59
"wlan0",
60
"wlan1",
61
"wifi0",
62
"ath0",
63
"ath1",
64
"ppp0",
65
]
66
for ifname in interfaces:
67
try:
68
ip = get_interface_ip(ifname)
69
break
70
except OSError:
71
pass
72
return ip
73
74