2017-02-08 146 views
0

我使用python中的請求庫進行HTTP請求,但我需要從服務器響應http請求的ip地址,我試圖避免進行兩個調用(並可能有一個不同的ip地址從如何使用請求庫從http請求獲取IP地址?在python 2.7

作出答覆請求的人。這可能嗎?任何的Python HTTP庫是否允許我這樣做嗎?

我還使用代理的請求

+2

可能重複[在python中獲取url的IP地址嗎?](http://stackoverflow.com/questions/8370361/get-ip-address-of-url-in-python) – LaundroMat

+0

我得到這個錯誤@LaundroMat文件「search.py​​」,第58行 print socket.gethostbyname(page) ^ IndentationError:意外的縮進我正在使用這行打印socket.gethostbyname(page)內部循環 –

+0

這是一個縮進錯誤。你可以粘貼你的代碼[在repl.it](https://repl.it/languages/python)並分享它? – LaundroMat

回答

0

你可以使用插座GET IP地址 示例:我將使用Socket +請求獲取狀態碼+ ip地址:

import socket 
import requests 


url = 'www.google.com' 
sock = socket.gethostbyname(url) 
url2 = 'http://www.google.com' 
request = requests.get(url2) 
if request.status_code == 200: 
    status = 'HTTP 200 : OK' 
elif request.status_code == 302: 
    status = 'HTTP 302 : Redirect' 
elif request.status_code == 404: 
    status = 'HTTP 404 : NOT FOUND' 
else: 
    status = 'Unknow Status' 
print "[+] URL : " + url + "\n" 
print "[+] IP Addres : " + sock + "\n" 
print "[+] Reponse Status : " + status