小编Cor*_*erg的帖子

HTTP请求超时

在Python 2.6中,一个新的"timeout"参数被添加到httplib.HTTPConnection类:http://docs.python.org/library/httplib.html#httplib.HTTPConnection

但是,这只是连接到服务器的超时.我期待为请求设置超时值,而不是连接.httplib似乎不支持这一点.

有没有办法模仿这种行为?

python http

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

将Python字典压缩并一起列出

是否可以将python字典和列表一起压缩?

例如:

dict = {'A':1, 'B':2, 'C':3}
num_list = [1, 2, 3]
zipped = zip(dict, num_list)
Run Code Online (Sandbox Code Playgroud)

然后我想做这样的事情:

for key, value, num_list_entry in zipped:
  print key
  print value
  print num_list_entry
Run Code Online (Sandbox Code Playgroud)

我还没有找到解决方案,所以我想知道如何去做?

python dictionary list

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

SimpleXMLRPCServer的静音输出

我正在使用stdlib中的SimpleXMLRPCServer运行xml-rpc服务器.

我的代码看起来像这样:

import SimpleXMLRPCServer
import socket

class RemoteStarter:
    def start(self):
        return 'foo'

rs = RemoteStarter()
host = socket.gethostbyaddr(socket.gethostname())[0]
port = 9000
server = SimpleXMLRPCServer.SimpleXMLRPCServer((host, port))
server.register_instance(rs)
server.serve_forever()
Run Code Online (Sandbox Code Playgroud)

每次远程调用'start'方法时,服务器都会打印一条访问行,如下所示:

<server_name> - - [10/Mar/2010 13:06:20] "POST /RPC2 HTTP/1.0" 200 -
Run Code Online (Sandbox Code Playgroud)

我无法找到一种方法来使输出静音,因此它不会将这些访问行打印到stdout.

任何人?

python xml-rpc

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

如何使用python下载html页面后面的文件?

我正在尝试使用python从互联网上下载文件.我试过这段代码:

import urllib.requests
URL = 'http://www.mediafire.com/download/raju14e8aq6azbo/Getting+Started+with+MediaFire.pdf'
filename = "file.pdf"
urllib.request.urlretrieve(URL,filename)
Run Code Online (Sandbox Code Playgroud)

和:

from urllib.request import urlopen
from shutil import copyfileobj

URL = 'http://www.mediafire.com/download/raju14e8aq6azbo/Getting+Started+with+MediaFire.pdf'
filename = "file.pdf"
with urlopen(URL) as in_stream, open(filename, 'wb') as out_file:
    copyfileobj(in_stream, out_file)
Run Code Online (Sandbox Code Playgroud)

(我在最后的代码中找到了:使用什么命令代替urllib.request.urlretrieve?)

问题是这段代码下载了一个html文档而不是我需要的名为"MediaFire.pdf入门"的.pdf文件!我正在寻找一种方法来下载html页面后面提供的文件.

有什么建议吗?

python file download

0
推荐指数
1
解决办法
1670
查看次数

标签 统计

python ×4

dictionary ×1

download ×1

file ×1

http ×1

list ×1

xml-rpc ×1