㈠ 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
import
fcntl
import
struct
def
get_ip_address(ifname):
s
=
socket.socket(socket.af_inet,
socket.sock_dgram)
return
socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915,
#
siocgifaddr
struct.pack('256s',
ifname[:15])
)[20:24])
#get_ip_address('lo')环回地址
#get_ip_address('eth0')主机ip地址
㈢ python获取本地ip的方法
方法一可能有的获取不到ip(比如没有正确设置up名称)
方法一
import socket
#获取计算机名称
hostname=socket.gethostname()
#获取本机IP
ip=socket.gethostbyname(hostname)
print(ip)
方法二
import socket
def get_host_ip():
"""
查询本机ip地址
:return:
"""
try: s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.connect(('8.8.8.8',80))
ip=s.getsockname()[0]
finally:
s.close()
return ip
if __name__ == '__main__':
print(get_host_ip())
㈣ 如何用Python获取本机ip
import socket
localIP = socket.gethostbyname(socket.gethostname())#得到本地ip
print "local ip:%s "%localIP
㈤ python 批量处理(通过ip地址获取机器名)
importsys,socket
try:
ipLines=open('ip.txt').readlines()
results=[]
foripinipLines:
result=socket.gethostbyaddr(ip.replace(' ',''))
results.append(result[0])
print"Primaryhostname:"
print""+result[0]
print" Addresses:"
foriteminresult[2]:
print""+item
wr=open('hostname.txt','w+')
forhostinresults:
wr.write(host)
wr.write(' ')
wr.close()
exceptsocket.herror,e:
print"Couldn'tlookupname:",e
㈥ 在python中怎么匹配IP地址
㈦ 如何用python脚本获取ip,掩码,地址,网段
#!/usr/bin/python
importfcntl,socket,struct
defgetHwAddr(ifname):
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
info=fcntl.ioctl(s.fileno(),0x8927,struct.pack('256s',ifname[:15]))
return':'.join(['%02x'%ord(char)forcharininfo[18:24]])
printgetHwAddr('eth0')
㈧ python使用Flask框架获取用户IP地址的方法
主要介绍了python使用Flask框架获取用户IP地址的方法,实例分析了Python使用Flask框架remote_addr获取IP的`技巧,非常具有实用价值,需要的朋友可以参考下。
下面的代码包含了html页面和python代码,非常详细,如果你正使用Flask,也可以学习一下最基本的Flask使用方法。
python代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
from flask import Flask, render_template, request
# Initialize the Flask application
app = Flask(__name__)
# Default route, print user's IP
@app.route('/')
def index():
ip = request.remote_addr
return render_template('index.html', user_ip=ip)
if __name__ == '__main__':
app.run(
host="0.0.0.0",
port=int("80")
)
html代码如下:
?
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
29
30
31
32
<!DOCTYPE html>
<html lang="en">
<head>
<link href="bootstrap/3.0.0/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<p class="container">
<p class="header">
<h3 class="text-muted">How To Get The IP Address Of The User</h3>
</p>
<hr/>
<p>
You IP address is: <strong>{{user_ip}}</strong>
<p class="header">
<h3 class="text-muted">Code to retrieve the IP</h3>
</p>
<hr/>
<pre>
from flask import Flask, render_template, request
# Initialize the Flask application
app = Flask(__name__)
# Default route, print user's IP
@app.route('/')
def index():
ip = request.remote_addr
return render_template('index.html', user_ip=ip)
</pre>
</p>
</p>
</body>
</html>
希望本文所述对大家的Python程序设计有所帮助。
㈨ python 代理ip爬取,ip代理,数据库存储,去重,验证。
首先是ip_request.py
这个模块是对ip的一个验证,随机生成header头。
ip_to_mysql.py
本文是对ip进行存储,和提取ip,删除ip的操作。
这个是对日志的一个封装,这样用起来方便些。
总结,目前整体程序可优化的空间很大,这算是1.0版本
㈩ 如何使用Python实现爬虫代理IP池
第一步:找IP资源
IP资源并不丰富,换句话说是供不应求的,因此一般是使用动态IP。
免费方法,直接在网络上找,在搜索引擎中一搜索特别多能够提供IP资源的网站,进行采集即可。
付费方法,通过购买芝麻ip上的IP资源,并进行提取,搭建IP池。