㈠ 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池。