小编gci*_*ani的帖子

WinError 10061 - 无法建立连接

我正在调试一个过去有效的简单程序.我已经挑出了发生错误的指令,但我无法弄清楚是什么触发它.我已经阅读了与WinError 10061相关的所有问题,但我没有看到明确的答案

urllib.request.urlopen('http://www.wikipedia.org/')
Traceback (most recent call last):
File "C:\Python33\lib\urllib\request.py", line 1248, in do_open    h.request(req.get_method(), req.selector, req.data, headers)
File "C:\Python33\lib\http\client.py", line 1061, in request    self._send_request(method, url, body, headers)
File "C:\Python33\lib\http\client.py", line 1099, in _send_request    self.endheaders(body)
File "C:\Python33\lib\http\client.py", line 1057, in endheaders  self._send_output(message_body)
File "C:\Python33\lib\http\client.py", line 902, in _send_output    self.send(msg)
File "C:\Python33\lib\http\client.py", line 840, in send    self.connect()
File "C:\Python33\lib\http\client.py", line 818, in connect    self.timeout, self.source_address)
File "C:\Python33\lib\socket.py", line 435, in create_connection    raise err
File "C:\Python33\lib\socket.py", line 426, in create_connection    sock.connect(sa)
ConnectionRefusedError: …
Run Code Online (Sandbox Code Playgroud)

python-3.x

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

Python HTTP 异常处理

我正在运行一个从网站下载文件的程序。我已经介绍了一个异常处理 urllib.error.HTTPError,但现在我不时遇到其他我不确定如何捕获的错误:http.client.IncompleteRead。我是否只需将以下内容添加到底部的代码中?

except http.client.IncompleteRead:
Run Code Online (Sandbox Code Playgroud)

我必须添加多少个例外才能确保程序不会停止?并且我是否必须将它们全部添加到同一个Except 语句或多个Except 语句中。

try:
   # Open a file object for the webpage 
   f = urllib.request.urlopen(imageURL)
   # Open the local file where you will store the image
   imageF = open('{0}{1}{2}{3}'.format(dirImages, imageName, imageNumber, extension), 'wb')
   # Write the image to the local file
   imageF.write(f.read())
   # Clean up
   imageF.close()
   f.close()

except urllib.error.HTTPError: # The 'except' block executes if an HTTPError is thrown by the try block, then the program continues as usual.
   print ("Image fetch failed.")
Run Code Online (Sandbox Code Playgroud)

python exception-handling

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

Matlab将矢量转换为二进制矩阵

我有一个大小为(m,1)的向量v,其元素是从1:n中选取的整数.我想创建一个大小为(m,n)的矩阵M,如果v(i)= j则其元素M(i,j)为1,否则为0.我不想使用循环,并希望仅将其实现为简单的向量矩阵操作.

所以我首先考虑创建一个包含重复元素的矩阵

 M = v * ones(1,n) % this is a (m,n) matrix of repeated v
Run Code Online (Sandbox Code Playgroud)

例如,v = [1,1,3,2]'m = 4且n = 3

M =
     1     1     1
     1     1     1
     3     3     3
     2     2     2
Run Code Online (Sandbox Code Playgroud)

然后我需要创建一个大小为(1,n)的比较向量c

c = 1:n
1 2 3
Run Code Online (Sandbox Code Playgroud)

然后我需要进行一系列逻辑比较

M(1,:)==c % this results in [1,0,0]
.
M(4,:)==c % this results in [0,1,0]
Run Code Online (Sandbox Code Playgroud)

但是,我认为应该可以在紧凑矩阵表示法中执行遍历每一行的最后步骤,但是我很难理解并且对索引不够了解.最终结果应该是

M =
     1     0     0
     1     0     0
     0     0     1
     0     1     0
Run Code Online (Sandbox Code Playgroud)

arrays matlab vector matrix matrix-indexing

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