- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Observium 在本地主机上提取 Nginx 统计信息,但它返回“405 Not Allowed”:
# curl -I localhost/nginx_status
HTTP/1.1 405 Not Allowed
Server: nginx
Date: Wed, 19 Jun 2013 22:12:37 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 166
Connection: keep-alive
Keep-Alive: timeout=5
# curl -I -H "Host: example.com" localhost/nginx_status
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 19 Jun 2013 22:12:43 GMT
Content-Type: text/plain
Connection: keep-alive
Keep-Alive: timeout=5
能否请您告知如何在 Python 中使用“urllib2.urlopen”添加主机 header (Python 2.6.6):
当前脚本:
#!/usr/bin/env python
import urllib2
import re
data = urllib2.urlopen('http://localhost/nginx_status').read()
params = {}
for line in data.split("\n"):
smallstat = re.match(r"\s?Reading:\s(.*)\sWriting:\s(.*)\sWaiting:\s(.*)$", line)
req = re.match(r"\s+(\d+)\s+(\d+)\s+(\d+)", line)
if smallstat:
params["Reading"] = smallstat.group(1)
params["Writing"] = smallstat.group(2)
params["Waiting"] = smallstat.group(3)
elif req:
params["Requests"] = req.group(3)
else:
pass
dataorder = [
"Active",
"Reading",
"Writing",
"Waiting",
"Requests"
]
print "<<<nginx>>>\n";
for param in dataorder:
if param == "Active":
Active = int(params["Reading"]) + int(params["Writing"]) + int(params["Waiting"])
print Active
else:
print params[param]
最佳答案
您可能想查看 urllib2 missing manual了解更多信息,但基本上您创建一个标题标签和值的字典并将其传递给 urllib2.Request
方法。链接手册中代码的(略微)修改版本:
from urllib import urlencode
from urllib2 import Request urlopen
# Define values that we'll pass to our urllib and urllib2 methods
url = 'http://www.something.com/blah'
user_host = 'example.com'
values = {'name' : 'Engineero', # dict of keys and values for our POST data
'location' : 'Interwebs',
'language' : 'Python' }
headers = { 'Host' : user_host } # dict of keys and values for our header
# Set up our request, execute, and read
data = urlencode(values) # encode for sending URL request
req = Request(url, data, headers) # make POST request to url with data and headers
response = urlopen(req) # get the response from the server
the_page = response.read() # read the response from the server
# Do other stuff with the response
关于python - 'urllib2.urlopen' 添加主机 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202364/
我有一些代码使用 mechanize 和 beautifulsoup 来抓取一些数据。该代码在测试机器上运行良好,但生产机器正在阻止连接。我得到的错误是: urlopen error [Errno 1
我有一个正在测试的简单网站。它在本地主机上运行,我可以在我的网络浏览器中访问它。索引页就是简单的“运行”二字。 urllib.urlopen 将成功读取页面,但 urllib2.urlopen 不
我只是想更好地了解这里发生了什么,我当然可以使用 urllib2 来“解决”这个问题。 import urllib import urllib2 url = "http://www.crutchfie
import urllib print urllib.urlopen('http://www.reefgeek.com/equipment/Controllers_&_Monitors/Neptune
我刚刚开始编写 Python,并且遇到了 urllib 似乎是 Amazon.com 独有的问题。如果我做类似的事情: pageIn = urllib.request.urlopen("http://
我想模拟模块中对 urllib.request.urlopen 的调用。当它是单个文件时它可以工作,但是当我把它放在一个包中并在包的 __init__.py 中导入模块时,我不能再模拟它了。 复制 假
# Get the content type of a URL def get_url_type(url: str) -> str: r = urlopen(url) header = r.h
我尝试将现有 URL 作为参数传递,以将其 HTML 加载到单个 txt 文件中: for line in open('C:\Users\me\Desktop\URLS-HERE.txt'): if
我对使用 Request、urlopen 和 JSONDecoder().decode() 有点困惑。 目前我有: hdr = {'User-agent' : 'anything'} # heade
此代码用于打印网页。它现在打印一个空格,即使在浏览器中查看时该页面清楚地包含内容 from urllib.request import urlopen f = urlopen('http://onli
我刚买了 synology NAS (DS213J),我想在上面运行 python 脚本。 我的 python 脚本: 1 #!/opt/bin/python 2 3 import url
我在名为 urlopen_test() 的函数中使用 urllib.urlopen()。在这个函数中我调用了两次 urlopen,第一次调用很快,但第二次调用速度很快。有人打电话来帮我弄清楚为什么吗?
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎是题外话,因为它缺乏足够的信息来诊断问题。 更详细地描述您的问题或include a min
1.data参数 data是可选的,需要使用bytes()方法将参数转化为字节编码格式的内容。如果传递了这个参数,请求方式就不是GET方式,而是POST方式。
我正在使用 迭代保存在 http 网站上的 .dat 文件 import urllib2 test_file = urllib2.urlopen('http://~/file.dat') 然后,我有一
这是我的问题: import urllib2 response=urllib2.urlopen('http://proxy-heaven.blogspot.com/') html=response.r
我没有使用 python 的经验,也没有使用下面的代码打开 url 并读取响应的经验。我收到未经授权的错误,因为该网站使用 Windows 身份验证。有人可以提供有关如何发送用户名和密码的代码示例吗?
我希望我的程序尝试打开页面并在时间间隔[0,t]内获取数据,如果该时间到期,连接应该关闭。 我正在使用urllib2来尝试完成任务。 t=1 url="http://example.com" resp
我正在尝试使用 python 程序填写表格,它对于某些网站效果很好,但对于这个特定的网站则不然,我不知道为什么。 这是代码片段 query = { 'adults':'1', 'children':'
当我使用 urllib2.urlopen() 时,我在考虑它只是为了标题读取还是实际上带回了整个网页? IE 是否在调用 urlopen 或调用 read() 时实际获取 HTML 页面? handl
我是一名优秀的程序员,十分优秀!