urllib是否加密认证数据?

2 投票
1 回答
963 浏览
提问于 2025-04-17 15:04

如果我用Python的urllib做以下操作,这样安全吗?

username = raw_input("Enter your username: ")
password = getpass.getpass("And password: ")
auth = urllib.urlencode({"username": username,"password": password})
validated = urllib.urlopen('https://loginhere.com', auth)

在用户的电脑和本地网络之间,能不能被监视HTTP请求流量的人看到密码?或者说urllib会对登录数据进行加密吗?

我查看了urllib的文档,看到有关于不检查https证书的警告,但没看到关于加密的内容。

1 个回答

1

urllib并不加密任何东西,它只是使用了从socket类传过来的SSL库。urllib本身只是按照你定义的方式发送数据。

要验证SSL,可以使用:

import urllib2

try:
    response = urllib2.urlopen('https://example.com') 
    print 'response headers: "%s"' % response.info()
except IOError, e:
    if hasattr(e, 'code'): # HTTPError
        print 'http error code: ', e.code
    elif hasattr(e, 'reason'): # URLError
        print "can't connect, reason: ", e.reason
    else:
        raise

撰写回答