Path: blob/trunk/py/test/selenium/webdriver/common/network.py
1865 views
# Licensed to the Software Freedom Conservancy (SFC) under one1# or more contributor license agreements. See the NOTICE file2# distributed with this work for additional information3# regarding copyright ownership. The SFC licenses this file4# to you under the Apache License, Version 2.0 (the5# "License"); you may not use this file except in compliance6# with the License. You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing,11# software distributed under the License is distributed on an12# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13# KIND, either express or implied. See the License for the14# specific language governing permissions and limitations15# under the License.1617# module for getting the lan ip address of the computer18import os19import socket2021if os.name != "nt":22import fcntl23import struct2425def get_interface_ip(ifname):26def _bytes(value, encoding):27return bytes(value, encoding)2829sckt = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)30return socket.inet_ntoa(31fcntl.ioctl(sckt.fileno(), 0x8915, struct.pack("256s", _bytes(ifname[:15], "utf-8")))[20:24] # SIOCGIFADDR32)333435def get_lan_ip():36if os.environ.get("CI") == "true":37return "0.0.0.0"3839try:40ip = socket.gethostbyname(socket.gethostname())41except Exception:42return "0.0.0.0"43if ip.startswith("127.") and os.name != "nt":44interfaces = [45"eth0",46"eth1",47"eth2",48"en0",49"en1",50"en2",51"en3",52"en4",53"eno0",54"eno1",55"eno2",56"eno3",57"eno4",58"wlan0",59"wlan1",60"wifi0",61"ath0",62"ath1",63"ppp0",64]65for ifname in interfaces:66try:67ip = get_interface_ip(ifname)68break69except OSError:70pass71return ip727374