ctx = ssl.create_default_context()作用
时间: 2024-05-17 09:15:07 浏览: 9
这行代码的作用是创建一个 SSL 上下文对象。SSL (Secure Sockets Layer) 是一种安全通信协议,它使用加密技术来保护网络连接中传输的数据。在 Python 中,使用 SSL 上下文对象可以在网络连接中实现传输加密和身份验证等安全特性。create_default_context() 是 Python 3.2 版本引入的函数,可以创建一个默认配置的 SSL 上下文对象,通常在创建 HTTPS 连接时使用。
相关问题
将下面代码改为GO代码host = 'https://kuaidi100.market.alicloudapi.com' path = '/getExpress' method = 'GET' appcode = '你自己的AppCode' querys = 'NO=12345678&TYPE=yuantong' bodys = {} url = host + path + '?' + querys request = urllib2.Request(url) request.add_header('Authorization', 'APPCODE ' + appcode) ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE response = urllib2.urlopen(request, context=ctx) content = response.read() if (content): print(content)
以下是将代码改为GO代码的示例:
```go
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
host := "https://kuaidi100.market.alicloudapi.com"
path := "/getExpress"
method := "GET"
appcode := "你自己的AppCode"
querys := "NO=12345678&TYPE=yuantong"
url := host + path + "?" + querys
req, err := http.NewRequest(method, url, nil)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Authorization", "APPCODE " + appcode)
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
```
请注意,由于GO语言的标准库中已经包含了HTTP客户端,因此我们不需要像Python中一样使用`urllib2`和`ssl`模块,而是直接使用GO语言的`http`包发送HTTP请求,并使用`ioutil`包读取响应内容。
pymysql.connect 通讯加密_Python通过SSL加密连接MySQL
要通过SSL加密连接MySQL,需要在连接MySQL时使用SSL参数。具体步骤如下:
1. 安装PyMySQL模块
```python
pip install pymysql
```
2. 导入PyMySQL模块和ssl模块
```python
import pymysql
import ssl
```
3. 创建SSL上下文
```python
ssl_ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ssl_ctx.load_cert_chain(certfile='server-cert.pem', keyfile='server-key.pem')
```
其中,'server-cert.pem'和'server-key.pem'分别是MySQL服务器的证书和私钥路径。
4. 连接MySQL并指定SSL参数
```python
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='password',
db='test',
ssl=ssl_ctx
)
```
其中,ssl参数指定SSL上下文。
5. 执行SQL语句
```python
with conn.cursor() as cursor:
cursor.execute('SELECT * FROM users')
result = cursor.fetchall()
print(result)
```
完整代码示例:
```python
import pymysql
import ssl
ssl_ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
ssl_ctx.load_cert_chain(certfile='server-cert.pem', keyfile='server-key.pem')
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
password='password',
db='test',
ssl=ssl_ctx
)
with conn.cursor() as cursor:
cursor.execute('SELECT * FROM users')
result = cursor.fetchall()
print(result)
conn.close()
```
注意:在MySQL服务器上启用SSL连接需要在my.cnf文件中配置。具体可参考MySQL官方文档。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)