ip更换

Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6EmUbbW&id=564564604865

import requests
import time
import sys 
import os.path

"List of free ip services"
"http://icanhazip.com/"
"http://checkip.dyndns.org/"
"https://freegeoip.net/json/"
"http://www.telize.com/ip"
"http://ip-api.com/json"
"http://curlmyip.com/"
"http://ipinfo.io/ip"

#Global variables
ipFile="/tmp/ip.log"
timeout = 10

class Service:
  url=""
  def requestself): return requests.getself.url, timeout = timeout)

class IcanhazipService):
  name="icanhazip"
  url="http://icanhazip.com/"
  def ipself): return self.request).text.strip)

class FreegeoipService):
  name="freegeoip"
  url="https://freegeoip.net/json/"
  def ipself): return self.request).json)["ip"]

class TelizeService):
  name="telize"
  url="http://www.telize.com/ip"
  def ipself): return self.request).text.strip)

class IpApiService):
  name="ip-api"
  url="http://ip-api.com/json"
  def ipself): return self.request).json)["query"]

class IfconfigService):
  name="ifconfig.me"
  url="http://ifconfig.me/all.json"
  def ipself): return self.request).json)["ip_addr"]

def request_ip):
  #List of services
  services = [Icanhazip), Freegeoip), Telize), IpApi), Ifconfig) ]
  for i in rangelenservices)):
    
    service = services[i]
    try:
      start = time.time)
      print "* Requesting current ip with '{}'".formatservice.name)
      ip = service.ip)
      print "* Request took {} seconds ".formatinttime.time) - start))
      return ip
    except Exception as error:
      print "* Exception when requesting ip using '{}': {} ".formatservice.name, error )
      
  error = "Non available services, add more services or increase the timeout services = {}, timeout = {}) ".formatlenservices), timeout)
  raise RuntimeErrorerror)

def current_ip):
  return openipFile,"r").readlines)[0]

def save_ipip):
  f = openipFile,'w')
  f.writestrip)) 

#Main
if os.path.isfileipFile) : #File exists
  request_ip = request_ip)  
  current_ip = current_ip)

  if request_ip != current_ip:
    save_iprequest_ip)
    print "* IP has changed from {} to {}".formatcurrent_ip, request_ip)
    sys.exit1)
  else :
    print "* IP is still the same: {}".formatcurrent_ip)

else: 
  request_ip = request_ip)
  save_iprequest_ip)
  print "* This is the first time to run the ip_change script, I will create a file in {} to store your current address: {} ".formatipFile, request_ip)

#Test
"""
services = [Icanhazip), Freegeoip), Telize), IpApi), Ifconfig) ]
for i in rangelenservices)):
  service = services[i]
  print "{} ip result: {} ".formatservice.name, service.ip) )
"""

  

Published by

风君子

独自遨游何稽首 揭天掀地慰生平