python 获取网站IP以及服务器

写了一个识别网站的ip以及服务器的代码,比较烂,希望大佬指教

 1 import requests
 2 import sys
 3 import socket
 4
 5 def get_ip(url):            # 获取IP
 6     if ‘http‘ in str(url):
 7         url = url.split(‘//‘)[1]
 8         ip = socket.gethostbyname(url)
 9     else:
10         ip = socket.gethostbyname(url)
11     return ip
12
13 def get_server(url):    # 获取服务器
14     if ‘http‘ in str(url):
15         http_url = url
16         https_url = url
17     else:
18         http_url = ‘http://‘ + str(url)
19         https_url = ‘https://‘ + str(url)
20     try:
21         re_header = requests.get(http_url).headers
22     except:
23         re_header = requests.get(https_url).headers
24     try:
25         server = re_header[‘server‘]
26     except: server = ‘unknow‘
27     return server
28
29
30 if __name__ == "__main__":
31     if sys.argv[1] == ‘-r‘:
32         with open(sys.argv[2]) as f:
33             for line in f:
34                 line = line.strip()
35                 try:
36                     target_ip = get_ip(line)
37                     server = get_server(line)
38                     print("website:", line)
39                     print("-------------IP:"+target_ip+"-------------")
40                     print(‘Server:‘, server,‘\n‘)
41                 except : print("website:", line, "can‘t visit\n")
42     else:
43         url = sys.argv[1]
44         try:
45             target_ip = get_ip(url)
46             server = get_server(url)
47             print("website:", url)
48             print("-------------IP:"+target_ip+"-------------")
49             print(‘Server:‘, server,‘\n‘)
50         except : print("website:", url, "can‘t visit\n")

用了requests,socket,sys 标准库

对单个网站进行操作:

对存放文本文件进行操作:

时间: 2024-11-05 18:56:56

python 获取网站IP以及服务器的相关文章

Python网络编程小例子:使用python获取网站域名信息

Whois简介 whois(读作"Who is",非缩写)是用来查询域名的IP以及所有者等信息的传输协议.简单说,whois就是一个用来查询域名是否已经被注册,以及注册域名的详细信息的数据库(如域名所有人.域名注册商).通过whois来实现对域名信息的查询.早期的whois查询多以命令列接口存在,但是现在出现了一些网页接口简化的线上查询工具,可以一次向不同的数据库查询.网页接口的查询工具仍然依赖whois协议向服务器发送查询请求,命令列接口的工具仍然被系统管理员广泛使用.whois通常

python 获取自身ip

原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- import socket import fcntl import struct def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcnt

java获取客户端IP和服务器IP

一.Java获取服务器IP(转自:https://www.cnblogs.com/george93/p/6306579.html) //方法一,也是最简单的一种. String SERVER_IP = InetAddress.getLocalHost().getHostAdress().toString(); //方法二: /** * 获取服务器IP地址 * @return */ @SuppressWarnings("unchecked") public static String g

Python获取客户端IP地址

获取客户端IP地址 if 'HTTP_X_FORWARDED_FOR' in request.META: ipaddress = request.META['HTTP_X_FORWARDED_FOR'] else: ipaddress = request.META['REMOTE_ADDR'] 原文地址:https://blog.51cto.com/14284354/2401482

python获取本地ip

def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl(s.fileno(),0x8915,struct.pack('256s', ifname[:15]))[20:24]) 1.套接字 2.通常,非Python程序以32位字节包的形式存储和使用IP地址.inet_aton(ip_addr)和inet_ntoa(pack

python获取本地IP地址发送邮件

#!/usr/bin/env python #_*_coding:utf-8 _*_ import time import socket import fcntl import struct import smtplib from email.mime.text import MIMEText def get_ip_add(ifname): s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) return socket.inet_ntoa(fcnt

python获取代理IP

利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup from threading import Thread headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'} #定义获取IP函数

python获取网站信息

#coding:utf-8 import urllib2 import os import sys reload(sys) sys.setdefaultencoding("utf-8") from bs4 import BeautifulSoup heads = {} heads['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0

python 获取远程设备ip地址

python2.7 #!/usr/bin/env python # Python Network Programming Cookbook -- Chapter - 1 # This program is optimized for Python 2.7. # It may run on any other version with/without modifications. import socket def get_remote_machine_info(): remote_host =