python 培训之HTTP

时间:2021-10-09 10:25:08

1. urllib

#!/usr/env/python
# -*- coding:UTF-8 -*-
from __future__ import print_function
import sys
import urllib2
import json
import click
import socket def get_ipinfo(ip):
baseurl = "http://ip.taobao.com/service/getIpInfo.php?ip={}"
url = baseurl.format(ip)
response = urllib2.urlopen(url)
json_data = response.read()
dict_data = json.loads(json_data)
info = dict_data["data"]
print(",".join([info["ip"],info["country"],
info["area"],info["city"]]))
return info def find_ip(name):
try:
ip = socket.gethostbyname(name)
except:
ip = "127.0.0.1"
return ip

2. request

3. wget (ubuntu shell bash)