程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Using Python to complete IP survival detection

編輯:Python

IP Survival tests

Recently, we are building our own IP pool , Among them, it is getting IP Post screening survival IP The methods are summarized as follows .

There are three main ways :

  • Use telnetlib modular
  • Use urllib modular
  • Use requests modular

The following is a brief introduction to the above three methods :

Use telnetlib Module detection IP viability ( Not recommended )

import telnetlib
ip = '195.170.38.230'
port = '8080'
try:
res = telnetlib.Telnet(ip, port, timeout=10)
print(res)
except Exception as e:
print('>>>', e)
print('ip invalidate')

Use urllib modular

from urllib import request
link = 'https://www.baidu.com'
# Notice the proxy_ip yes mapping In the form of eg: {key: value}, Among them, if port No 80 Need to write 
proxy_ip = {
'http': '195.170.38.230:8080'}
proxy_support = request.ProxyHandler(proxy_ip)
opener = request.build_opener(proxy_support)
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36')]
res = request.urlopen(link)
print(res)

Use requests modular

import requests
headers = {

'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15'
}
link = 'https://www.baidu.com'
# Similarly, if port Not by default 80 Need to be written separately 
proxies = {
'http': '195.170.38.230:8080'}
print(' Being tested ip:', real_ip)
result = requests.get(link, headers=headers, proxies=proxies, timeout=10)
if response.status_code not in SUCCESS_CODES:
print('>>>{} Invalid ip'.format(real_ip))
else:
print('success>>>{} ip It works '.format(real_ip))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved