【发布时间】:2012-08-13 20:54:55
【问题描述】:
我想通过“.cst”文件连接到网络设备。如果你想在浏览器中打开它,你必须输入
http://x.x.x.x/index.cst?Lang=en&login=blafoo&passwd=foobla
如何使用 urllib 或其他包发送此请求?
求助坦克
巴斯蒂
【问题讨论】:
-
你试过了吗?有什么问题?
我想通过“.cst”文件连接到网络设备。如果你想在浏览器中打开它,你必须输入
http://x.x.x.x/index.cst?Lang=en&login=blafoo&passwd=foobla
如何使用 urllib 或其他包发送此请求?
求助坦克
巴斯蒂
【问题讨论】:
import urllib.request
import urllib.parse
params = urllib.parse.urlencode({'Lang': 'en', 'login': 'blafoo', 'passwd': 'foobla'})
f = urllib.request.urlopen("http://x.x.x.x/index.cst?%s" % params)
f.read()
【讨论】:
与urllib:
import urllib
site = urllib.urlopen('http://x.x.x.x/index.cst?Lang=en&login=blafoo&passwd=foobla')
data = site.read()
此脚本的变量data 将存储您从所传递的 URL 获得的内容(响应正文)。
【讨论】:
我建议使用requests(所有酷孩子都使用它!;),尽管已经给出了使用urllib 的答案。有要求:
import requests
response = requests.get('http://x.x.x.x/index.cst?Lang=en&login=blafoo&passwd=foobla')
# response.text contains the response contents
# response.status_code gives the response status code (200, 201, 404, etc)
额外学分:
import requests
data = {'Lang': 'en', 'login': 'blafoo', 'passwd': 'foobla'}
response = requests.get('http://x.x.x.x/index.cst', params=data)
【讨论】: