init
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python
|
||||
import sys,os,socket
|
||||
def IsOpen(ip,port):
|
||||
socket.setdefaulttimeout(5)
|
||||
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
|
||||
try:
|
||||
s.connect((ip,int(port)))
|
||||
s.shutdown(2)
|
||||
print(True)
|
||||
except:
|
||||
print(False)
|
||||
if __name__ == '__main__':
|
||||
IsOpen(sys.argv[1],int(sys.argv[2]))
|
||||
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
import socket
|
||||
def Get_local_ip():
|
||||
"""
|
||||
Returns the actual ip of the local machine.
|
||||
This code figures out what source address would be used if some traffic
|
||||
were to be sent out to some well known address on the Internet. In this
|
||||
case, a Google DNS server is used, but the specific address does not
|
||||
matter much. No traffic is actually sent.
|
||||
"""
|
||||
try:
|
||||
socket.setdefaulttimeout(5)
|
||||
csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
csock.connect(('1.1.1.1', 80))
|
||||
(addr, port) = csock.getsockname()
|
||||
csock.close()
|
||||
return addr
|
||||
except socket.error:
|
||||
return "127.0.0.1"
|
||||
|
||||
if __name__ == "__main__":
|
||||
IPADDR = Get_local_ip()
|
||||
print(IPADDR)
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python
|
||||
#coding:utf-8
|
||||
import sys,socket,json
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
import urllib2 as request
|
||||
else:
|
||||
import urllib.request as request
|
||||
|
||||
try:
|
||||
socket.setdefaulttimeout(5)
|
||||
if len(sys.argv) == 1:
|
||||
apiurl = "http://ip-api.com/json"
|
||||
elif len(sys.argv) == 2:
|
||||
apiurl = "http://ip-api.com/json/%s" % sys.argv[1]
|
||||
content = request.urlopen(apiurl).read().decode('utf-8')
|
||||
content = json.JSONDecoder().decode(content)
|
||||
#print(content)
|
||||
if content['status'] == 'success':
|
||||
if content['country'] == 'China':
|
||||
print("CN")
|
||||
else:
|
||||
print(content['country'])
|
||||
except:
|
||||
print("Usage:%s IP" % sys.argv[0])
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env python
|
||||
import sys,re,socket
|
||||
|
||||
if sys.version_info[0] == 2:
|
||||
import urllib2 as request
|
||||
else:
|
||||
import urllib.request as request
|
||||
|
||||
class Get_public_ip:
|
||||
socket.setdefaulttimeout(5)
|
||||
def getip(self):
|
||||
try:
|
||||
myip = self.visit("http://ipv4.icanhazip.com/")
|
||||
except:
|
||||
try:
|
||||
myip = self.visit("http://pv.sohu.com/cityjson?ie=utf-8")
|
||||
except:
|
||||
myip = "So sorry!!!"
|
||||
return myip
|
||||
def visit(self,url):
|
||||
opener = request.urlopen(url)
|
||||
if url == opener.geturl():
|
||||
str = opener.read().decode('utf-8')
|
||||
return re.search('\d+\.\d+\.\d+\.\d+',str).group(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
getmyip = Get_public_ip()
|
||||
print(getmyip.getip())
|
||||
Reference in New Issue
Block a user