在Python脚本中检查包是否安装的好方法是什么?我知道解释器很容易,但是我需要在脚本中完成它.
我想我可以检查系统中是否有安装过程中创建的目录,但我觉得有更好的方法.我正在尝试确保安装了Skype4Py软件包,如果没有,我会安装它.
我完成检查的想法
我正在编写一小段代码,用于读取存储在.pem文件中的公钥和私钥.我使用以下命令来生成密钥.
下面的命令生成一对密钥.
$openssl genrsa -out mykey.pem 2048
Run Code Online (Sandbox Code Playgroud)
此命令用于生成私钥
$openssl pkcs8 -topk8 -inform PEM -outform PEM -in mykey.pem \
-out private_key.pem -nocrypt
Run Code Online (Sandbox Code Playgroud)
和此命令获取公钥.
$ openssl rsa -in mykey.pem -pubout -outform DER -out public_key.der
Run Code Online (Sandbox Code Playgroud)
我写了两个分别读取私钥和公钥的方法.
public PrivateKey getPemPrivateKey(String filename, String algorithm) throws Exception {
File f = new File(filename);
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int) f.length()];
dis.readFully(keyBytes);
dis.close();
String temp = new String(keyBytes);
String privKeyPEM = temp.replace("-----BEGIN PRIVATE KEY-----\n", "");
privKeyPEM = privKeyPEM.replace("-----END …Run Code Online (Sandbox Code Playgroud) 我使用下面的openssl命令将我的公钥存储到.pem文件中.
openssl> x509 -in E:/mycert.pem -pubkey -out E:/mypubkey.pem
Run Code Online (Sandbox Code Playgroud)
但是当我尝试使用此命令时,它将整个证书信息存储在mypubkey.pem文件中.
我已经看到我可以使用保存我的公钥
openssl> x509 -pubkey -noout -in cert.pem > pubkey.pem
Run Code Online (Sandbox Code Playgroud)
但这是一个错误.我不能使用">"运算符.
在DynamoDB中将一个表格相同地复制到新表格的最佳方法是什么?
(我不担心原子性).
我认为我想在bootstrap移动版本中隐藏网格中的一列是有点棘手的.让我们举个例子
<div class="row">
<div class="col-xs-9">
<p class="testimonial-about">"We have managed to received good number of applications through MyJobs in comparison to advertising in the newspaper and would recommend MyJobs to other companies to assist with their recruitment needs"</p>
</div>
<div class="col-xs-3 center-image hidden-xs"><img src="myimage.png"/></div>
</div>
Run Code Online (Sandbox Code Playgroud)
在编码之上,我在移动设备查看时隐藏图像部分.我想要的是我不想要图像部分的间距,即使它被隐藏为增加左边部分直到右边.
我在非代理模式下从API网关调用基于Python的AWS Lambda方法.我应该如何正确处理异常,以便使用部分异常设置适当的HTTP状态代码和JSON主体.
作为一个例子,我有以下处理程序:
def my_handler(event, context):
try:
s3conn.head_object(Bucket='my_bucket', Key='my_filename')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
raise ClientException("Key '{}' not found".format(filename))
# or: return "Key '{}' not found".format(filename) ?
class ClientException(Exception):
pass
Run Code Online (Sandbox Code Playgroud)
我应该抛出异常还是返回一个字符串?那我该如何配置集成响应?显然我有RTFM,但FM是FU.
我正在使用 Debian/Ubuntu,并且尝试在我的 Python 虚拟环境中安装 Airflow 提供程序:
\n$ pip install apache-airflow-providers-mysql\n error: subprocess-exited-with-error\n\n \xc3\x97 Getting requirements to build wheel did not run successfully.\n \xe2\x94\x82 exit code: 1\n \xe2\x95\xb0\xe2\x94\x80> [24 lines of output]\n /bin/sh: 1: pkg-config: not found\n /bin/sh: 1: pkg-config: not found\n Trying pkg-config --exists mysqlclient\n Command 'pkg-config --exists mysqlclient' returned non-zero exit status 127.\n Trying pkg-config --exists mariadb\n Command 'pkg-config --exists mariadb' returned non-zero exit status 127.\n Traceback (most recent call last):\n File "/home/user/airflow/airflow_env/lib/python3.10/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>\n main()\n …Run Code Online (Sandbox Code Playgroud) 我有一个Python脚本,它作为Windows服务运行.该脚本分叉另一个进程:
with subprocess.Popen( args=[self.exec_path], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as proc:
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
OSError: [WinError 6] The handle is invalid
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 911, in __init__
File "C:\Program Files (x86)\Python35-32\lib\subprocess.py", line 1117, in _get_handles
Run Code Online (Sandbox Code Playgroud) 当我返回UTF8字符时,我在Debian Stable php5(5.4.39-0 + deb7u1)上遇到JsonResponse问题.
我在Debian Testing php5(5.6.6 + dfsg-2)上开发了一个应用程序,下面的代码就像一个魅力:
$response = new JsonResponse();
$response->headers->set('Content-Type', 'application/json');
$response->setData($data);
return $response;
Run Code Online (Sandbox Code Playgroud)
但是在部署到稳定的prod服务器之后,我开始为完全相同的DB/Data字符集等获得以下异常:
request.CRITICAL: Uncaught PHP Exception InvalidArgumentException: "Malformed UTF-8 characters,
possibly incorrectly encoded." at /site/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/JsonResponse.php
line 123 {"exception":"[object] (InvalidArgumentException(code: 0):
Malformed UTF-8 characters, possibly incorrectly encoded. at
/site/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/JsonResponse.php:123)"} []
Run Code Online (Sandbox Code Playgroud)
作为$ data DO传递的DB的响应包含我无法控制的UTF8字符.我只需要显示它们.
我想我遇到了5.4的bug,但是我怎么能轻松地走动呢?我试过了:
$response = new JsonResponse();
$response->headers->set('Content-Type', 'application/json');
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
$response->setData($data);
return $response;
Run Code Online (Sandbox Code Playgroud)
但我得到了同样的错误.
想法?
我想在python中使用枚举,如下面的代码(java).我是Python的新手.我在Java中有以下代码,并希望在Python中复制功能:
class Direction {
public enum Direction {LEFT, RIGHT, UP, DOWN}
public void navigate(Direction direction)
switch(direction){
case Direction.LEFT:
System.out.print("left");
break;
case Direction.RIGHT:
System.out.print("right");
break;
case Direction.UP:
System.out.print("up");
break;
case Direction.DOWN:
System.out.print("down");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制用户只提供Python方法的枚举?
python ×4
openssl ×2
pem ×2
airflow ×1
aws-lambda ×1
css ×1
debian ×1
enums ×1
java ×1
jsonresponse ×1
mysql-python ×1
package ×1
php ×1
pkcs#8 ×1
python-2.7 ×1
python-3.x ×1
skype ×1
subprocess ×1
symfony ×1
ubuntu ×1
utf-8 ×1
windows ×1
x509 ×1