配置 json 数据库连接,天杀的密码里面有个 @,咋整呢?试了前面加\没能转义成

2019-02-09 01:18:11 +08:00
 sjmcefc2

"DataSource": "postgres://mmuser:pg@most@localhost:5432/mattermost?sslmode=disable\u0026connect_timeout=10",

这种密码 pg@most 怎么 转义呢

4038 次点击
所在节点    程序员
10 条回复
meik2333
2019-02-09 01:20:57 +08:00
不用转义啊,直接用不行么?
eslizn
2019-02-09 01:25:29 +08:00
urlencode
eslizn
2019-02-09 01:25:59 +08:00
这个和 json 没半毛钱关系 这是 uri 的编码问题
whi147
2019-02-09 08:28:43 +08:00
base64 编码
biggray0312
2019-02-09 08:34:51 +08:00
楼主试试 2 楼说的,url encode
abcdabcd987
2019-02-09 12:09:30 +08:00
楼主不妨试试看下面的这两种方法:

"DataSource": "user=mmuser password='pg@most' dbname=mattermost sslmode=disable connect_timeout=10"
"DataSource": "postgres://mmuser:'pg@most'@localhost:5432/mattermost?sslmode=disable\u0026connect_timeout=10"


下面附上解题思路:

1. 从楼主的配置中可以看到一个关键词叫做 mattermost
2. Google "mattermost" 可以找到 GitHub: https://github.com/mattermost/mattermost-server
3. 在 GitHub 顶端搜索 DataSource 可以找到建立数据库连接的代码: https://github.com/mattermost/mattermost-server/blob/2ca222033cfd1d10f5e72228356665e4dbe31a55/store/sqlstore/supplier.go#L207
4. 往上翻可以看到 dbsql 是 "database/sql"
5. 不是很懂 golang,所以 Google 搜索了一下 "golang database/sql",找到了文档 https://golang.org/pkg/database/sql/#Open
6. 发现这是在 golang 标准库里面的,但是文档上面说标准库不包含任何驱动,"See https://golang.org/s/sqldrivers for a list of third-party drivers"
7. 从上面的列表里面可以看到有个 Postgres 的驱动: https://github.com/lib/pq
8. 从 lib/pq 的 GitHub 可以找到文档 https://godoc.org/github.com/lib/pq
9. 从 lib/pq 的文档中可以看到使用这个库的办法是导入 "github.com/lib/pq",于是在 mattermost 的 GitHub 里面搜索 "github.com/lib/pq" 可以验证确实用的是这个库 https://github.com/mattermost/mattermost-server/blob/26684716aae02c9bd1ce4dea14f42c6f36f718c8/store/sqlstore/store.go#L8
10. 从 lib/pq 的文档中可以看到,可以用单引号把值包起来,而且 lib/pq 支持两个格式的 connection string

不知道能不能成功,楼主加油!
tinyproxy
2019-02-09 12:13:36 +08:00
URL 不用\转义,有兴趣找找 RFC 看看,没兴趣粘贴下面的代码改改就知道该输入啥了。

```python
#!/usr/bin/env python3
from urllib.parse import quote_plus
username = 'foo'
password = 'bar@host:123'


url = 'postgres://%s:%s@HOST:PORT' % (quote_plus(username), quote_plus(password))
print(url)

```
sutra
2019-02-09 14:52:41 +08:00
try this: pg%40most
sjmcefc2
2019-02-09 16:03:14 +08:00
@abcdabcd987 太棒了。恭喜发财。
E1n
2019-02-10 09:32:59 +08:00
@abcdabcd987 优秀。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/533515

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX