小编AK4*_*K47的帖子

python selenium driver.quit() 在 except 块内

我的应用程序是这样的,如果它抛出异常,我希望驱动程序关闭,我尝试了以下代码,但它抛出异常。

我的代码:其中 url 是我要打开的网址

driver=webdriver.Firefox()
try:
   driver.get(url)

except:

   driver.quit()
Run Code Online (Sandbox Code Playgroud)

它正在关闭驱动程序但抛出异常。我应该如何解决这个问题?

这是我的堆栈跟踪

raceback (most recent call last):
  File "/folderpath", line 47, in <module>
    driver.close()
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 505, in close
    self.execute(Command.CLOSE)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 231, in execute
    response = self.command_executor.execute(driver_command, params)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
    return self._request(command_info[0], url, body=data)
  File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 425, in _request
    self._conn.request(method, parsed_url.path, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 973, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line …
Run Code Online (Sandbox Code Playgroud)

python exception selenium-webdriver

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

在条件中调用max的列表理解的大O:O(n)还是O(n ^ 2)?

问:编写一个返回数组中第二大数字的算法

a = [1, 2, 3, 4, 5]
print(max([x for x in a if x != max(a)]))
>> 4
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚这个算法是如何工作的,以及pythons内部魔术是否会使它像写一个线性算法一样高效,这个算法只是在列表上循环a一次并存储最高和第二高的值.

如我错了请纠正我:

  • 呼吁max(a)将是O(n)

  • [x for x in a] 也是O(n)

python是否足够智能来缓存值,max(a)或者这是否意味着算法的列表理解部分是O(n ^ 2)?

然后最终max([listcomp])将是另一个O(n),但这只会在理解完成后运行一次,所以最终算法将是O(n ^ 2)?

内部是否有任何花哨的业务会缓存该max(a)值并导致该算法的运行速度比O(n ^ 2)快?

python algorithm performance big-o list-comprehension

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

“名称”在Django-URL中是什么意思?

我想知道,当使用url(时from django.conf.urls import url),是什么name = 'insert-something'意思?例如,当制作一个用于注册新用户的url时:

url(r'^register', views.register, name='register')
Run Code Online (Sandbox Code Playgroud)

名称=“注册”在这里是什么意思?为什么有必要?

谢谢!

python django

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

/ usr / bin / python:在mac下没有名为pip的模块/在mac中安装python包

我正在尝试在Mac计算机下使用带有可视代码的python

我有此错误消息:

/usr/bin/python -m pip install pylint
user:~ user$ /usr/bin/python -m pip install pylint
/usr/bin/python: No module named pip
Run Code Online (Sandbox Code Playgroud)

我找到了一些帮助,然后尝试以下方法:

user:~ user$ easy_install pip
error: can't create or remove files in install directory

The following error occurred while trying to add or remove files in the
installation directory:

    [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-19813.pth'

The installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:

    /Library/Python/2.7/site-packages/
Run Code Online (Sandbox Code Playgroud)

我不明白为什么我有这个:

[Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-19813.pth'
Run Code Online (Sandbox Code Playgroud)

正如我将这一行放在我的bash_profile中

# Setting PATH for Python …
Run Code Online (Sandbox Code Playgroud)

python macos

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

使用远程 FTP 原子包和私钥发出与服务器的连接

我正在尝试使用原子包“Remote-FTP”并使用私钥连接到我的服务器。

我在我的服务器上设置了 SSH 密钥,并且可以使用腻子成功连接。

私钥保存在我的项目文件夹中,我有一个现有的 .ftpconfig 文件(没有私钥),当我替换文件中的信息时,该文件成功连接。

密钥已转换为必要的 ssh 格式,目前我没有密码。我也给出了私钥文件的完整路径

这是我的 .ftpconfig 文件 -

{
"protocol": "sftp",
"host": "user@server",
"port": 22,
"user": "",
"pass": "",
"promptForPass": false,
"remote": "",
"local": "",
"agent": "",
"privatekey": "putty_private_key_sbappsupconvrted.ppk",
"passphrase": "",
"hosthash": "",
"ignorehost": true,
"connTimeout": 10000,
"keepalive": 10000,
"keyboardInteractive": true,
"watch": [],
"watchTimeout": 500
}
Run Code Online (Sandbox Code Playgroud)

我收到的错误代码是:

远程 FTP:连接...

远程 FTP:连接失败错误:getaddrinfo EAI_FAIL user@server:22

远程 FTP:连接已关闭

我也给出了私钥文件的完整路径

C:\Users\user\.atom\server\putty_private_key_sbappsupconvrted.ppk
Run Code Online (Sandbox Code Playgroud)

我收到这个错误

无法解析 .ftpconfig Unexpected token U in JSON at position 219

任何帮助将不胜感激

ftp sftp remote-access ssh-keys private-key

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

如何在Python中使用双重反斜杠替换单个字符串

我想在Python中将双反斜杠替换成单个字符串.

例如,有一个字节字符串.

word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
Run Code Online (Sandbox Code Playgroud)

我需要这个字节字符串.

word = b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
Run Code Online (Sandbox Code Playgroud)

如果我使用替换像:

word = word.replace(b"\\",b"\")
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误.

File "test.py", line 79
word = word.replace(b"\\", b"\")
                               ^
SyntaxError: EOL while scanning string literal
Run Code Online (Sandbox Code Playgroud)

有谁知道怎么做?

python byte

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

Python while循环不按预期工作

当我在输入中键入"no"时,我希望它将"1"添加到"x",从而结束循环,但会发生的是忽略它并且不添加1 x.这是代码.

x = 1
password = ""

while x == 1:        
    # imagine there is some code here which works

    ans1 = input("\n\nTest a new password? ")
    ans1 = ans1.upper()

    print(ans1)

    if ans1 == ("Y" or "YES"):
        x = x
    elif ans1 == ("N" or "NO"):
        x = x + 10

    print(x)
Run Code Online (Sandbox Code Playgroud)

这是底部if/elif语句不起作用.它应该继续再次请求输入,直到用户说"不",但这不起作用.

python loops while-loop

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

使用Python填充csv文件

以下代码段创建了一个CSV文件,但每隔一行都是空白的.如何防止这些换行符发生?

import datetime
import time
import csv

i = 0
while i < 10:
    TempProbe = "78.12" 
    CurrentTime = time.strftime("%x")
    CurrentDate = time.strftime("%I:%M:%S")
    stringAll = TempProbe + "," + CurrentTime + "," + CurrentDate
    print(stringAll)
    file = open("outFile.csv", "a")
    csvWriter = csv.writer( file )
    csvWriter.writerow( [TempProbe, CurrentTime,CurrentDate] )
    file.close()
    i = i + 1
    time.sleep(1)
Run Code Online (Sandbox Code Playgroud)

python csv

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

如何在angular 2中使用Handsontable库

我是npm中配置部分的新手,我试图在使用angular-cli(ng init)创建的angular 2项目中使用handsontable库。我为它添加了打字稿定义。

这是我的app.compponent.ts

import { Component } from '@angular/core';
import 'handsontable/dist/handsontable.full.js';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app works!';
  ngOnInit() {
  }
  ngAfterViewInit() {
    const myData = [
            ["1", "2", "3", "4", "5", "6"]
        ];

        const config = {
            data: myData,
            colHeaders: ['A', 'B', 'C', 'D', 'E', 'F'],
            startRows: 5,
            startCols: 5,
            minSpareCols: 1,
            //always keep at least 1 spare row at the right
            minSpareRows: 1
            //always keep at least 1 …
Run Code Online (Sandbox Code Playgroud)

npm handsontable typescript angular2-components angular2-cli

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

如何使用私钥加密 PBEWithHmacSHA512AndAES_128 创建 PKCS12 密钥库

我需要在 Scala/Java 中构建一个 PKCS-12 文件,并且我希望使用基于 AES 的私钥加密(例如 PBEWithHmacSHA512AndAES_128)

我使用此代码(取自此处

val outputStream = new FileOutputStream(file)
val salt = new Array[Byte](20)
new SecureRandom().nextBytes(salt)
val kspkcs12 = KeyStore.getInstance("PKCS12")
kspkcs12.load(null, null)
kspkcs12.setEntry("test", new KeyStore.PrivateKeyEntry(keys.getPrivate, Array(cert)),
  new KeyStore.PasswordProtection("changeMe".toCharArray, "PBEWithHmacSHA512AndAES_128", new PBEParameterSpec(salt,
    100000)))
kspkcs12.store(outputStream, "changeMe".toArray)
Run Code Online (Sandbox Code Playgroud)

现在,当检查结果时

 openssl pkcs12 -info -in filename.p12  -noout
Run Code Online (Sandbox Code Playgroud)

我越来越:

MAC:sha1 Iteration 100000 PKCS7 Data Shrouded Keybag: PBES2<unsupported parameters> PKCS7 Encrypted data: pbe​​WithSHA1And40BitRC2-CBC, Iteration 50000 Certificate bag'

为什么我得到的是“PKCS7 加密数据”而不是 PKCS12?我从

“PBES2<不支持的参数>”

提供者不支持请求的算法的消息。是否有 PKCS12 提供程序?

java encryption keystore pkcs#12 p12

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