我在Python 2.6.x上遇到以下错误,我应该如何解决这个问题?
except (httplib.IncompleteRead), e:
NameError: name 'httplib' is not defined
Run Code Online (Sandbox Code Playgroud) 这是为python 2.7.10也许我没有正确使用try..except..finally阻止.
我需要查看从网页上获得的HTTP响应代码.如果我得到200代码,一切正常.如果我得到任何其他代码,请报告代码.
如果我得到200 HTTP代码,它工作正常.如果我得到一个异常,由于某种原因,它给了我一个UnboundedLocalError,说明我的变量没有被引用.如何让我的变量在finally块中被识别?
这是我的代码:
try:
conn = httplib.HTTPConnection(host, port=9000)
conn.request("GET", "/alive")
resp = conn.getresponse().status
except Exception as e:
print "got some exception"
print "exception " + e
print "Exception msg: " + e.message
print "Args for exception: " + e.args
finally:
if resp == 200:
print "got a 200 http response. everything seems good"
if resp != 200:
print "didn't get a 200 http response. something wrong"
print "this is the code we got: " + str(resp)
Run Code Online (Sandbox Code Playgroud)
这是我们得到的输出,如果它与http …
我使用python httplib来实现REST api以连接Django tastypie.但每当我尝试获取状态代码时,它都会显示以下错误
AttributeError at /actions/login
HTTPResponse instance has no attribute 'status_code'
Run Code Online (Sandbox Code Playgroud)
我的代码如下
import hashlib
import hmac
from django.shortcuts import render_to_response
from django.template import RequestContext
def loginAction(request):
username=request.POST['email']
password=request.POST['password']
import httplib, urllib
params = urllib.urlencode({'username': username})
#hash username here to authenticate
digest=hmac.new("qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50", str(request.POST['password']),hashlib.sha1).hexdigest()
auth=username+":"+digest
headers = {"Content-type": "application/json","Accept": "text/plain","Authorization":auth}
conn = httplib.HTTPConnection("localhost",8000)
conn.request("POST", "/api/ecp/profile/", params, headers)
conn.set_debuglevel(1)
response = conn.getresponse()
return response
Run Code Online (Sandbox Code Playgroud) 不同的问题:1 脚本。我必须从将在客户端运行并在远程 URL 上执行 POST 的 Python 脚本进行 JSON-ify。我正在关注这里的文档http://docs.python.org/2/library/httplib.html - 但是,我不确定我是否做得对。当我在 Mac 上运行它时,我也没有得到任何响应状态。现在,我对以下内容存有疑问——
(1) The dummy 'device_key' (of the client)
(2) The IP, Port listed at HTTPConnection --- I don't want to hard-code the IP, Port - should I be using "ServerHost:Port"
(3) The cpuStats is a nametuple (of 'user'= somevalue, 'idle' = somevalue, etc.) that is converted to a dict by _asdict(). I want to just send the cpuStats (namedtuple) to the URLpage.
(4) The cpu_times_percent(percpu=True) is what …Run Code Online (Sandbox Code Playgroud)