當前位置:首頁 » 編程語言 » pythongethostbyname

pythongethostbyname

發布時間: 2022-05-04 01:26:11

python 怎麼獲取本機的外網ip

>>> import socket
>>> hostname = socket.gethostname()
>>> print hostname
LuciferYang.local
>>> ip = socket.gethostbyname(hostname)
>>> print ip
10.101.8.171
>>> ipList = socket.gethostbyname_ex(hostname)
>>> print ipList
('luciferyang.local', [], ['10.101.8.171'])

理論上,不是伺服器的話不用有直接外網IP到機器,辦公室環境或者家庭環境都是區域網環境,外網IP都在路由器上面

㈡ 用python編寫「輸入一個或多個網址返回ip地址」的代碼

import socket
ip=input()
ip=socket.gethostbyname(addr)

㈢ 為什麼用python獲取mac地址會變動

首先聲明,我本人還沒有研究出來問題的究竟。此處只是寫下我本人的一點小心得,大家一起進步。

因為我發現,使用uuid庫得到的mac地址,總有最後一位不對。所以,我就查看了python官方的uuid文檔,找到了問題的關鍵是調用UUID()的時候,會調用getnode()函數以得到物理地址。

這個是getnode()函數的定義:

我把它摘出來,考到下面。

def getnode(*, getters=None):
"""Get the hardware address as a 48-bit positive integer.

The first time this runs, it may launch a separate program, which could
be quite slow. If all attempts to obtain the hardware address fail, we
choose a random 48-bit number with its eighth bit set to 1 as recommended
in RFC 4122.
"""
global _node
if _node is not None:
return _node

if sys.platform == 'win32':
getters = _NODE_GETTERS_WIN32
else:
getters = _NODE_GETTERS_UNIX

for getter in getters + [_random_getnode]:
try:
_node = getter()
except:
continue
if (_node is not None) and (0 <= _node < (1 << 48)):
return _node
assert False, '_random_getnode() returned invalid value: {}'.format(_node)


我正在試圖通過研究這個問題來試圖研究。但同樣,我覺得我們可以直接讓python調用系統庫,從而執行系統自帶的命令:(類似於windows下cmd裡面"ipconfig -all"命令,或者ubuntu下terminal中"ifconfig"命令)。實現物理地址。之後,根據「短時間內該機器的網卡不會出現過大的變動這個前提」,我們可以根據返回內容,切片出我們需要的部分即可。

㈣ python3套接字udp設置接受數據超時

Sometimes,you need to manipulate the default values of certain properties of a socket library, for example, the socket timeout.

設定並獲取默認的套接字超時時間。

1.代碼

1 import socket
2
3
4 def test_socket_timeout():
5 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6 print("Default socket timeout: %s" % s.gettimeout())
7 # 獲取套接字默認超時時間
8 s.settimeout(100)
9 # 設置超時時間
10 print("Current socket timeout: %s" % s.gettimeout())
11 # 讀取修改後的套接字超時時間
12
13
14 if __name__ == '__main__':
15 test_socket_timeout()
2. AF_INET和SOCK_STREAM解釋

1 # 地址簇
2 # socket.AF_INET IPv4(默認)
3 # socket.AF_INET6 IPv6
4 # socket.AF_UNIX 只能夠用於單一的Unix系統進程間通信
5
6 # socket.SOCK_STREAM(數據流) 提供面向連接的穩定數據傳輸,即TCP/IP協議.多用於資料(如文件)傳送。
3.gettimeout()和settimeout()解釋

1 def gettimeout(self): # real signature unknown; restored from __doc__
2 """
3 gettimeout() -> timeout
4
5 Returns the timeout in seconds (float) associated with socket
6 operations. A timeout of None indicates that timeouts on socket
7 operations are disabled.
8 """
9 return timeout
10
11
12 def settimeout(self, timeout): # real signature unknown; restored from __doc__
13 """
14 settimeout(timeout)
15
16 Set a timeout on socket operations. 'timeout' can be a float,
17 giving in seconds, or None. Setting a timeout of None disables
18 the timeout feature and is equivalent to setblocking(1).
19 Setting a timeout of zero is the same as setblocking(0).
20 """
21 pass
22 # 設置套接字操作的超時期,timeout是一個浮點數,單位是秒。值為None表示沒有超時期。
23 # 一般,超時期應該在剛創建套接字時設置,因為它們可能用於連接的操作(如 client 連接最多等待5s )
4.運行結果

1 Default socket timeout: None
2 Current socket timeout: 100.0

㈤ Python有沒有gethostbyname的方法

importsocket
printsocket.gethostbyname(『xxx』)

㈥ python中gethostname得到的主機名是啥

importsocket
socket.gethostname()
#'hostname'

主機名就是計算機的名字(計算機名),網上鄰居就是根據主機名來識別的。這個名字可以隨時更改,在windows操作系統中,從我的電腦屬性的計算機名就可更改。

需要注意的是,主機名和用戶登錄名稱無關。

㈦ 如何使用多線程python掃描二級子域名

日站沒什麼好辦法了往往也會想到其二級域名,於是寫了一個比較簡陋的掃描二級域名的程序
速度一般般,不過如果線程開多了還是可以的
源程序(subdomain.py):

#! /usr/bin/env python
#coding=utf-8
import threading , Queue, optparse, os
import pycurl, StringIO, msvcrt, socket

queue = Queue.Queue()

class ScanThread(threading.Thread):

def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue

def run(self):
while 1:
p = self.queue.get()
if p is None:
break
try:
sub_domain = p+'.'+domain
crl = pycurl.Curl()
crl.fa = StringIO.StringIO()
crl.setopt(pycurl.URL,sub_domain)
crl.setopt(pycurl.VERBOSE,0)
crl.setopt(pycurl.FOLLOWLOCATION,1)
crl.setopt(pycurl.MAXREDIRS,5)
crl.setopt(pycurl.CONNECTTIMEOUT, 60)
crl.setopt(pycurl.TIMEOUT, 300)
crl.setopt(crl.WRITEFUNCTION,crl.fa.write)
try:
crl.perform()
ip=socket.gethostbyname(sub_domain)
print sub_domain, ip
content = sub_domain+' '+ip+'\n'
self.writefile(wfile, 'a+', content)
except:
pass
except:
print "error"
self.writefile('F:/py/Domain/log.txt', 'a+', p+'\n')

queue.task_done()

def writefile(self, path, type, content):
f = open(path, type)
f.write(content)
f.close

class ThreadGetKey(threading.Thread):
def run(self):
while 1:
try:
chr = msvcrt.getch()
if chr == 'q':
print "stopped by your action ( q )"
os._exit(1)
else:
continue
except:
os._exit(1)

# now starting...

def main():
parser = optparse.OptionParser('Usages: %prog -d <domain> -r <read> -w <write> -t <thread(s)>')
parser.add_option('-d',dest='domain',type='string',help='the url to query')
parser.add_option('-r',dest='read',type='string',help='the dic file to read default=F:/py/Domain/dic.txt', default='F:/py/Domain/dic.txt')
parser.add_option('-w',dest='write',type='string',help='save the reasults to the catalogue \
default=F:/py/Domain/results.txt', default='F:/py/Domain/results.txt')
parser.add_option('-t',dest='threads',type='int',help='set the thread(s) default=10',default=10)
(options,args) = parser.parse_args()
if options.domain == None:
使用方法:
python subdomain.py -d .com -r dic.txt -w results.txt -t 50
主要影響速度的是這一塊代碼:

try:
crl.perform()
ip=socket.gethostbyname(sub_domain)
print sub_domain, ip
content = sub_domain+' '+ip+'\n'
self.writefile(wfile, 'a+', content)
except:
pass

主要是一開始理解錯了,以為二級域名不存在和某個網頁不存在一樣會返回404代碼,於是想到用返回碼來判斷。
結果後來程序一直出錯,才發現當二級域名不存在的時候返回的是「未找到伺服器」,根本不存在返回碼,於是只能使用一個try來調試錯誤,主要速度也就被這里影響了。當然線程開多了也是可以看到刷屏效果的~~

㈧ 關於python 不能返回我的主機名

socket.gethostbyname(),需要一個參數的,而且這個不是返回主機名,是返回主機名對應的ip地址

你想要用的可能是
socket.gethostname() 函數

㈨ python socket通信 客戶端如何檢測同一 區域網內開啟服務功能的服務端ip地址

你是想讓客戶端輸出伺服器的IP地址嗎?你客戶端連接到伺服器之後,伺服器直接把ip發給客戶端不就可以了嗎?還是說你有多個伺服器,可是客戶端鏈接到哪個伺服器得看你客戶端的套接字的設置

㈩ python我的世界如何獲取身邊材質的名稱,並print

#!/usr/bin/env python
# Python Network Programming Cookbook -- Chapter - 1# This program is optimized for Python 2.7.
# It may run on any other version with/without modifications.

import socket

def print_machine_info():
host_name = socket.gethostname()
ip_address = socket.gethostbyname(host_name)
print "Host name: %s" %host_name
print "IP address: %s" %ip_addressif __name__ == '__main__':
print_machine_info()

熱點內容
境外伺服器租用怎麼辦 發布:2024-05-03 11:45:34 瀏覽:943
我的世界伺服器怎麼設置到重生點 發布:2024-05-03 11:33:04 瀏覽:752
mysqllinux安裝包下載 發布:2024-05-03 11:32:07 瀏覽:934
以太演算法的幣 發布:2024-05-03 11:30:45 瀏覽:383
編譯nfs 發布:2024-05-03 11:30:40 瀏覽:432
srs搭建直播伺服器 發布:2024-05-03 11:29:53 瀏覽:133
南通哪裡有賣密碼鎖的 發布:2024-05-03 11:22:01 瀏覽:522
除法指演算法 發布:2024-05-03 11:21:23 瀏覽:844
h265壓縮 發布:2024-05-03 11:20:27 瀏覽:490
手機相機自拍時怎麼看是否存儲 發布:2024-05-03 11:08:49 瀏覽:544