# encoding: utf-8

import asyncio
import requests
import functools url = "http://ip.zxinc.org/api.php" params_list = [] # 存放所有要查询的参数
with open("IPv6.txt", 'r', encoding='utf-8') as f:
for ip in f:
ip = ip.strip()
payload = {"type": "json", "ip": ip}
params_list.append(payload) def save_result(res): # task回调函数
print(res)
params, response = res.result() # 这就是get_ip_location函数的返回值,通过异步任务的result函数获取 if response.status_code == 500:
with open("save_result22.txt", "a+", encoding="utf-8") as f1:
f1.write(params["ip"] + " = 地址有误" + "\n")
else:
result = response.json()
country = result["data"]["country"]
with open("save_result22.txt", "a+", encoding="utf-8") as f1:
f1.write(params["ip"] + " = " + country + "\n") async def get_ip_location(param): # 发送get请求
print("start ", param)
# param = {"type": "json", "ip": "1.1.1.1"} param是requests.get的参数
loop = asyncio.get_event_loop()
try:
# 要使用await, 必须将第三方包转为awaitable对象
response = await loop.run_in_executor(None, functools.partial(requests.get, url=url, params=param))
return param, response
except Exception as e:
return param, e tasks = []
for param in params_list:
task = asyncio.ensure_future(get_ip_location(param)) # 1.创建异步任务
task.add_done_callback(save_result) # 2.给任务添加回调函数
tasks.append(task)
loop = asyncio.get_event_loop() # 3.创建事件循环
loop.run_until_complete(asyncio.wait(tasks)) # 4.执行所有异步任务

python36异步任务 获取ip地址的地理位置的更多相关文章

  1. php获取ip地址所在的地理位置的实现

    1,通过腾讯或者新浪提供的接口来获取(新浪和腾讯类似) <?php     function getIPLocation($queryIP){      $url = 'http://ip.qq ...

  2. Linux下Python获取IP地址

    <lnmp一键安装包>中需要获取ip地址,有2种情况:如果服务器只有私网地址没有公网地址,这个时候获取的IP(即私网地址)不能用来判断服务器的位置,于是取其网关地址用来判断服务器在国内还是 ...

  3. PHP 获取IP地址位置信息「聚合数据API」

    聚合数据 提供了[查询IP所属区域]的服务接口,只需要以 GET 请求的方式向 API 传入 IP地址 和 APPKEY 即可获得查询结果. 这里的难点主要在于如何通过PHP获取客户端IP地址,以及如 ...

  4. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  5. 【PHP开发篇】一个统计客户端商机提交的获取IP地址

    1.对客服提交数据的ip地址记录. 获取ip地址的方法: public function getIP() { global $ip; if (getenv("HTTP_X_REAL_IP&q ...

  6. 获取 IP 地址

    package j2se.core.net.base; import java.net.InetAddress;import java.net.UnknownHostException; public ...

  7. js获取IP地址方法总结_转

    js代码获取IP地址的方法,如何在js中取得客户端的IP地址.原文地址:js获取IP地址的三种方法 http://www.jbxue.com/article/11338.html 1,js取得IP地址 ...

  8. iOS 获取IP地址

    一.获取本机IP地址 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 #import <ifadd ...

  9. 【Qt】QT5 获取IP地址

    QT获取本机IP地址 #include <QtNetwork/QHostAddress> #include <QtNetwork/QNetworkInterface> #inc ...

随机推荐

  1. 如何关闭SQL进程

    --通过下面的查询得到trace ID select * from sys.traces --修改下面的@traceid参数,关闭,删除对应的trace exec sp_trace_setstatus ...

  2. kolla-ansible 重新部署 ceph 遇到的问题

    问题 TASK [ceph : Fetching Ceph keyrings] ******************************************* fatal: [controll ...

  3. 546. Remove Boxes

    Given several boxes with different colors represented by different positive numbers. You may experie ...

  4. 【OCP-12c】2019年CUUG OCP 071考试题库(78题)

    78.View the exhibit and examine the structure of the CUSTOMERStable. Which two tasks would require s ...

  5. “全栈2019”Java第一百章:局部内部类可以实现接口吗?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. soapui加载天气预报接口报错Characters larger than 4 bytes are not supported: byte 0xb1 implies a length of more than 4 byte的解决办法

    soapui加载天气预报接口时报错如下: Error loading [http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl ...

  7. 记一次升级Ubuntu内核

      uname -a 查看当前使用内核版本 升级指定内核  apt-get install linux-image-4.4.0-131-generic dpkg --get-selections | ...

  8. python --爬虫基础 --爬猫眼top 100 使用 requests 库的基本操作

    import requests import re import json import time def get_page(url): # 获取页数 headers = { 'User-Agent' ...

  9. python要点记录

    1.字典:当存储的key数目在几万到几十万之间效率最高.

  10. 题目1004:Median(查找中位数)

    问题来源 http://ac.jobdu.com/problem.php?pid=1004 问题描述 给你两个非降序序列,让你求中位数.中位数为第(n+1)/2个数(从0开始计算). 问题分析 这个问 ...