当前位置:首页 » 编程语言 » 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()

热点内容
方舟怎么用自己的存档进入别人的服务器 发布:2025-05-14 16:46:25 浏览:876
微博视频高清上传设置 发布:2025-05-14 16:38:41 浏览:548
数据库图书管理设计 发布:2025-05-14 16:33:52 浏览:378
php开发的网页 发布:2025-05-14 16:22:03 浏览:477
服务器内存跑满了怎么回事 发布:2025-05-14 16:21:16 浏览:224
微信qq音乐缓存 发布:2025-05-14 16:16:16 浏览:469
c语言回收内存 发布:2025-05-14 16:16:08 浏览:144
2021国产安卓顶级旗舰买哪个 发布:2025-05-14 16:15:36 浏览:300
linux自学视频 发布:2025-05-14 16:14:49 浏览:256
我的世界服务器崩了重启 发布:2025-05-14 16:09:37 浏览:45