python - 这个 python 脚本的 Windows 替代方案,用于 linux 从 URL 获取 IP

标签 python linux

我是 Python 的新手。所以在 newboston 的最新 python 教程中,我制作了一个网络爬虫。然而,此 Python 脚本仅适用于 Linux。 Windows 可以替代以下代码吗?

import os

def get_ip_address(url):
  command = "host " + url     
  process = os.popen(command)
  results = str(process.read())
  marker = results.find('has address') + 12
  return results[marker:].splitlines()[0]

print(get_ip_address('google.com'))

我想要它的格式与我想要编写多个模块(即获取 ip、谁是谁等)并将它们放在一起作为一个 Python 程序的格式相同。

我试图在结果中只获取 IP 地址,没有其他内容。

或者,这是我目前在 Windows 上拥有的:

import socket

def get_ips_for_host(url):
    try:
        ips = socket.gethostbyname_ex(url)
    except socket.gaierror:
        ips = []
    return ips

ips = get_ips_for_host('www.facebook.com')
print(repr(ips))

如何修改它以使其只获取 IP 地址?

Similarly how can I get the nmap scan and the 'who is'?

最佳答案

the_ip = get_ips_for_host('www.facebook.com')[-1][0]

关于python - 这个 python 脚本的 Windows 替代方案,用于 linux 从 URL 获取 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489786/

相关文章:

c - Linux C 上的 HINTERNET InternetOpenUrl()

node.js - 如何在 Shell 中运行命令 CTRL+Z?

linux - 我如何编写一个命令来显示 mac 地址并且每个地址后跟一个换行符?

python - Gunicorn 导入错误 : cannot import name 'ALREADY_HANDLED' from 'eventlet.wsgi' in docker

python - 如何阻止 bokeh 在 Jupyter Notebook 中打开新标签页?

python - 当一个部分已经为假时,带有 "and"/&& 的两部分条件是否停止?

python - 在 Python 中清除窗口中的图形

c - 什么是 SOCKET accept() 错误 errno 316?

python - 将一个 pandas 数据框中的值替换为另一个数据框中的值

c - 将 execvp 与程序的输入文件结合使用并将输出重定向到新文件