简体   繁体   English

从python中的IP地址获取响应

[英]Get response from an ip address in python


Given an ip, how can i make an http request to this ip in python? 给定一个IP,我如何在python中对此IP发出http请求?

For example, if i want to get a file names 't1.txt' from the server that resides on '8.8.8.8', how can i do so? 例如,如果我想从驻留在“ 8.8.8.8”上的服务器获取文件名“ t1.txt”,该怎么办? I've tried using httplib and urllib2. 我尝试使用httplib和urllib2。 (It's better if the proper way will be by using standard Python libs). (最好的方法是使用标准的Python库)。

Thanks a lot, Iko. 非常感谢,Iko。

import urllib
urllib.urlretrieve ("http://8.8.8.8/t1.txt", "t1.txt")

For simple url retrieval, urllib is perfectly OK (and is a standard Python library)... 对于简单的url检索, urllib完全可以(并且是标准的Python库)...

... but if you are looking for something more easy to use in more complex cases, you should take a look at request : ...但是,如果您要寻找在更复杂的情况下更易于使用的内容,则应查看request

import requests
r = requests.get('http://8.8.8.8/t1.txt')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM