标签: httplib

python httplib名称或服务未知

我正在尝试使用httplib将信用卡信息发送到authorize.net.当我尝试发布请求时,我得到以下回溯:

File "./lib/cgi_app.py", line 139, in run res = method()
File "/var/www/html/index.py", line 113, in ProcessRegistration conn.request("POST", "/gateway/transact.dll", mystring, headers)
File "/usr/local/lib/python2.7/httplib.py", line 946, in request self._send_request(method, url, body, headers)
File "/usr/local/lib/python2.7/httplib.py", line 987, in _send_request self.endheaders(body)
File "/usr/local/lib/python2.7/httplib.py", line 940, in endheaders self._send_output(message_body)
File "/usr/local/lib/python2.7/httplib.py", line 803, in _send_output self.send(msg)
File "/usr/local/lib/python2.7/httplib.py", line 755, in send self.connect()
File "/usr/local/lib/python2.7/httplib.py", line 1152, in connect self.timeout, self.source_address)
File "/usr/local/lib/python2.7/socket.py", line 567, in create_connection raise error, msg
gaierror: [Errno -2] Name or …
Run Code Online (Sandbox Code Playgroud)

python ssl httplib

7
推荐指数
3
解决办法
4万
查看次数

处理IncompleteRead,URLError

它是一个Web挖掘脚本.

def printer(q,missing):
    while 1:
        tmpurl=q.get()
        try:
            image=urllib2.urlopen(tmpurl).read()
        except httplib.HTTPException:
            missing.put(tmpurl)
            continue
        wf=open(tmpurl[-35:]+".jpg","wb")
        wf.write(image)
        wf.close()
Run Code Online (Sandbox Code Playgroud)

q是一个Queue()由Urls组成的``缺少一个空队列来收集错误提升网址

它由10个线程并行运行.

每次我跑这个,我得到了这个.

  File "C:\Python27\lib\socket.py", line 351, in read
    data = self._sock.recv(rbufsize)
  File "C:\Python27\lib\httplib.py", line 541, in read
    return self._read_chunked(amt)
  File "C:\Python27\lib\httplib.py", line 592, in _read_chunked
    value.append(self._safe_read(amt))
  File "C:\Python27\lib\httplib.py", line 649, in _safe_read
    raise IncompleteRead(''.join(s), amt)
IncompleteRead: IncompleteRead(5274 bytes read, 2918 more expected)
Run Code Online (Sandbox Code Playgroud)

但我确实使用了except......我尝试过其他类似的东西

httplib.IncompleteRead
urllib2.URLError
Run Code Online (Sandbox Code Playgroud)

甚至,

image=urllib2.urlopen(tmpurl,timeout=999999).read()
Run Code Online (Sandbox Code Playgroud)

但这都不起作用..

我怎么能抓住IncompleteReadURLError

python error-handling urllib2 httplib

7
推荐指数
1
解决办法
7012
查看次数

如何在 Python 中发布分块的编码数据

我正在尝试将分块编码的数据发布到 httpbin.org/post。我尝试了两个选项:Requests 和 httplib

使用请求

#!/usr/bin/env python

import requests

def gen():
        l = range(130)
        for i in l:
                yield '%d' % i

if __name__ == "__main__":
        url = 'http://httpbin.org/post'
        headers = {
                        'Transfer-encoding':'chunked',
                        'Cache-Control': 'no-cache',
                        'Connection': 'Keep-Alive',
                        #'User-Agent': 'ExpressionEncoder'
                }
        r = requests.post(url, headers = headers, data = gen())
        print r
Run Code Online (Sandbox Code Playgroud)

使用 httplib

#!/usr/bin/env python

import httplib
import os.path

if __name__ == "__main__":
        conn = httplib.HTTPConnection('httpbin.org')
        conn.connect()
        conn.putrequest('POST', '/post')
        conn.putheader('Transfer-Encoding', 'chunked')
        conn.putheader('Connection', 'Keep-Alive')
        conn.putheader('Cache-Control', 'no-cache')
        conn.endheaders()
        for i in range(130): …
Run Code Online (Sandbox Code Playgroud)

python http httplib chunked-encoding python-requests

7
推荐指数
1
解决办法
1万
查看次数

当content-type为"application/xml"时,如何使用httplib发布非ASCII字符

我在Python 2.7中实现了Pivotal Tracker API模块.该枢纽跟踪API期望POST数据是XML文档和"应用程序/ xml"的是内容类型.

我的代码使用urlib/httplib发布文档,如下所示:

    request = urllib2.Request(self.url, xml_request.toxml('utf-8') if xml_request else None, self.headers)
    obj = parse_xml(self.opener.open(request))
Run Code Online (Sandbox Code Playgroud)

当XML文本包含非ASCII字符时,这会产生异常:

File "/usr/lib/python2.7/httplib.py", line 951, in endheaders
  self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 809, in _send_output
  msg += message_body
exceptions.UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 89: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

就像我所看到的那样,httplib._send_output正在为消息有效负载创建一个ASCII字符串,大概是因为它希望数据是URL编码的(application/x-www-form-urlencoded).只要使用ASCII字符,它就可以与application/xml一起使用.

是否有一种直接的方式来发布包含非ASCII字符的应用程序/ xml数据,或者我将不得不跳过箍(例如使用Twistd和POST有效负载的自定义生产者)?

python django urllib2 pivotaltracker httplib

6
推荐指数
1
解决办法
4246
查看次数

在运行几个小时后,永久性的gaierror"名称解析暂时失败"

我有一个长期运行的python脚本,与upstart一起启动.这个脚本提出了很多请求.一切都运行良好,但几个小时后我开始永久性地为每个请求得到以下错误:

  File "/opt/a/a-env/local/lib/python2.7/site-packages/atom/client.py", line 119, in request
  File "/opt/a/a-env/local/lib/python2.7/site-packages/atom/http_core.py", line 420, in request
  File "/opt/a/a-env/local/lib/python2.7/site-packages/atom/http_core.py", line 489, in _http_request
  File "/usr/lib/python2.7/httplib.py", line 931, in endheaders
  File "/usr/lib/python2.7/httplib.py", line 794, in _send_output
  File "/usr/lib/python2.7/httplib.py", line 756, in send
  File "/usr/lib/python2.7/httplib.py", line 1134, in connect
  File "/usr/lib/python2.7/socket.py", line 553, in create_connection
gaierror: [Errno -3] Temporary failure in name resolution
Run Code Online (Sandbox Code Playgroud)

这不是名称解析或DNS的问题,因为简单的应用程序重新启动可以解决此问题.

我已经尝试了Python 2.6 a 2.7并且出现了同样的情况.

我在跑 Linux 2.6.35-30-virtual #61-Ubuntu SMP Tue Oct 11 18:26:36 UTC 2011 x86_64 GNU/Linux

有一些帖子有这个问题,但没有一个有简单的解释或解决方案:

python urllib httplib python-sockets getaddrinfo

6
推荐指数
1
解决办法
4194
查看次数

硒铬驱动程序httplib.badstatusline

我使用以下教程安装selenium,chr​​ome,pyvritualdisplay和xvfb:https://christopher.su/2015/selenium-chromedriver-ubuntu/

当我尝试运行python selenium脚本时,我在调用webdriver.Chrome()时出现此错误

我在Ubuntu 14.04.3 LTS上使用python2.7.6

Traceback (most recent call last):
 File "selenium_python_unittests/attempt_50.py", line 9, in <module>
driver = webdriver.Chrome()
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 69, in __init__
desired_capabilities=desired_capabilities)
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 92, in __init__
self.start_session(desired_capabilities, browser_profile)
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 234, in execute
response = self.command_executor.execute(driver_command, params)
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 407, in execute
return self._request(command_info[0], url, body=data)
  File "/home/sele/headless/local/lib/python2.7/site-packages/selenium/webdriver/remote/remote_connection.py", line 439, in _request
resp = self._conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1051, …
Run Code Online (Sandbox Code Playgroud)

selenium httplib python-2.7 xvfb selenium-chromedriver

6
推荐指数
1
解决办法
3236
查看次数

使用httlib的HTTPSConnection和PKCS#12证书时出错

我正在尝试使用httplib的HTTPSConnection进行客户端验证,使用PKCS#12证书.我知道证书很好,因为我可以在MSIE和Firefox中使用它连接到服务器.

这是我的连接功能(证书包含私钥).我把它简化为基础知识:

def connect(self, cert_file, host, usrname, passwd):
    self.cert_file = cert_file
    self.host = host

    self.conn = httplib.HTTPSConnection(host=self.host, port=self.port, key_file=cert_file, cert_file=cert_file)

    self.conn.putrequest('GET', 'pathnet/,DanaInfo=200.222.1.1+')
    self.conn.endheaders()
    retCreateCon = self.conn.getresponse()

    if is_verbose:
        print "Create HTTPS connection, " + retCreateCon.read()
Run Code Online (Sandbox Code Playgroud)

(注意:请不要对硬编码路径发表评论 - 我试图让它首先工作;之后我会做得很好.硬编码路径是正确的,因为我在MSIE和Firefox中连接它我更改了帖子的IP地址.)

当我尝试使用PKCS#12证书(.pfx文件)运行它时,我得到了似乎是openSSL错误.这是整个错误回溯:

  File "Usinghttplib_Test.py", line 175, in 
    t.connect(cert_file=opts["-keys"], host=host_name, usrname=opts["-username"], passwd=opts["-password"])
  File "Usinghttplib_Test.py", line 40, in connect
    self.conn.endheaders()
  File "c:\python26\lib\httplib.py", line 904, in endheaders
    self._send_output()
  File "c:\python26\lib\httplib.py", line 776, in _send_output
    self.send(msg)
  File "c:\python26\lib\httplib.py", line 735, in send
    self.connect()
  File "c:\python26\lib\httplib.py", line 1112, …

python authentication certificate pkcs#12 httplib

5
推荐指数
2
解决办法
5496
查看次数

如何使用httplib(python 2.6)处理超时?

我正在使用httplib通过https访问api,并且需要在api关闭的情况下构建异常处理.

这是一个示例连接:

connection = httplib.HTTPSConnection('non-existent-api.com', timeout=1)
connection.request('POST', '/request.api', xml, headers={'Content-Type': 'text/xml'})
response = connection.getresponse()
Run Code Online (Sandbox Code Playgroud)

这应该超时,所以我期待引发异常,并response.read()返回一个空字符串.

我怎么知道是否有超时?更好的是,优雅地处理第三方api问题的最佳方法是什么?

python httplib

5
推荐指数
2
解决办法
2万
查看次数

Python:httplib getresponse发出许多recv()调用

getresponserecv在读取HTML请求的标头时发出许多调用。实际上recv,它为每个字节发出,导致许多系统调用。如何进行优化?

我在具有strace dump的Ubuntu计算机上进行了验证。

样例代码:

conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD", "/index.html")
r1 = conn.getresponse()
Run Code Online (Sandbox Code Playgroud)

strace转储:

sendto(3, "HEAD /index.html HTTP/1.1\r\nHost:"..., 78, 0, NULL, 0) = 78
recvfrom(3, "H", 1, 0, NULL, NULL)      = 1
recvfrom(3, "T", 1, 0, NULL, NULL)      = 1
recvfrom(3, "T", 1, 0, NULL, NULL)      = 1
recvfrom(3, "P", 1, 0, NULL, NULL)      = 1
recvfrom(3, "/", 1, 0, NULL, NULL)      = 1
...
Run Code Online (Sandbox Code Playgroud)

python httplib python-2.7

5
推荐指数
1
解决办法
672
查看次数

url使用python 2.7进行编码

>>> import httplib
>>> x = httplib.HTTPConnection('localhost', 8080)
>>> x.connect()
>>> x.request('GET','/camera/store?fn=aaa&ts='+str.encode('2015-06-15T14:45:21.982600+00:00','ascii')+'&cam=ddd')
>>> y=x.getresponse()
>>> z=y.read()
>>> z
Run Code Online (Sandbox Code Playgroud)

'错误:格式无效:"2015-06-15T14:45:21.982600 00:00"格式错误为"00:00"

系统告诉我这个错误.由于我想将此格式编码为:2015-06-15T14%3A45%3A21.982600%2B00%3A00

rest python-2.x httplib

5
推荐指数
1
解决办法
6766
查看次数