麻烦大家帮忙修改下 关于python获取IP的代码……多谢
import httplib, urllib
import socket
import time
params = dict(
login_email="XXXX", # 替换为DNSPOD账号
login_password="XXXXX", # 替换为DNSPOD密码
format="json",
domain_id=XXXX, # 填写Domain ID
record_id=XXXXXXXXXX, # 填写Record ID
sub_domain="nas", # 填写二级域名
record_line="默认",
)
current_ip = None
def ddns(ip):
params.update(dict(value=ip))
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/json"}
conn = httplib.HTTPSConnection("dnsapi.cn")
conn.request("POST", "/Record.Ddns", urllib.urlencode(params), headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
print data
conn.close()
return response.status == 200
def getip():
sock = socket.create_connection(('ns1.dnspod.net', 6666))
ip = sock.recv(16)
sock.close()
return ip
if __name__ == '__main__':
while True:
try:
ip = getip()
print ip
if current_ip != ip:
if ddns(ip):
current_ip = ip
except Exception, e:
print e
pass
time.sleep(30)
Type "copyright", "credits" or "license()" for more information.
>>> import http.client
>>> conn = http.client.HTTPConnection("iframe.ip138.com")
>>> conn.request("GET", "/ic.asp")
>>> res = conn.getresponse()
>>> res.read()
b'<html>\r\n<head>\r\n<meta http-equiv="content-type" content="text/html; charset=gb2312">\r\n<title> \xc4\xfa\xb5\xc4IP\xb5\xd8\xd6\xb7 </title>\r\n</head>\r\n<body style="margin:0px"><center>\xc4\xfa\xb5\xc4IP\xca\xc7\xa3\xba[113.73.231.9] \xc0\xb4\xd7\xd4\xa3\xba\xb9\xe3\xb6\xab\xca\xa1\xd6\xd0\xc9\xbd\xca\xd0 \xb5\xe7\xd0\xc5</center></body></html>'
>>> import socket
>>> s = socket.socket(socket.AF_INET,socket. SOCK_STREAM)
>>> s.connect(("ns1.dnspod.net", 6666))
>>> s.recv(1024)
b'113.73.231.9'
>>>