python发送数据包获取返回数据(其他的语言能实现也可以)
要将下面的数据用python模拟,发送数据包后自动获取返回的数据,应该怎么写呢?发送[2017/6/911:30:27:673]POST/CloudUploadInfo/...
要将下面的数据用python模拟,发送数据包后自动获取返回的数据,应该怎么写呢?
发送
[2017/6/9 11:30:27:673]
POST /CloudUploadInfo/CloudRequest.aspx HTTP/1.1
Host: 192.168.20.95:8080
Accept: */*
Content-Type: application/x-www-form-urlencoded
(发送内容)?r=X/iEjVpsDA9TOhhdGA8I
返回
[2017/6/9 11:30:27:681]
HTTP/1.1 200 OK
Date: Fri, 09 Jun 2017 03:30:23 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=kuzrdcued4gthjqkwjkymyzv; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
(返回内容)ubm5ubm5Nm8gPio5N 展开
发送
[2017/6/9 11:30:27:673]
POST /CloudUploadInfo/CloudRequest.aspx HTTP/1.1
Host: 192.168.20.95:8080
Accept: */*
Content-Type: application/x-www-form-urlencoded
(发送内容)?r=X/iEjVpsDA9TOhhdGA8I
返回
[2017/6/9 11:30:27:681]
HTTP/1.1 200 OK
Date: Fri, 09 Jun 2017 03:30:23 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Set-Cookie: ASP.NET_SessionId=kuzrdcued4gthjqkwjkymyzv; path=/; HttpOnly
Cache-Control: private
Content-Type: text/html; charset=utf-8
(返回内容)ubm5ubm5Nm8gPio5N 展开
1个回答
2017-06-12
展开全部
使用百度翻译的一个例子,可以翻译你输入的文字,发送需要的内容,获取返回的数据
import urllib.request
import urllib.parse
import json
import time
while True:
url = 'http://fanyi.baidu.com/v2transapi'
words = input("输入你想翻译的内容:")
data = {}
data['from'] = 'auto'
data['to'] = 'auto'
data['query'] = words
data['transtype'] = 'translang'
data['simple_means_flag'] = '3'
data = urllib.parse.urlencode(data).encode('utf-8')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'
}
req = urllib.request.Request(url, data, headers)
req.add_header('Referer', 'http://fanyi.baidu.com/')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
js = json.loads(html)
# print(html)
# print(js)
print(js['trans_result']['data'][0]['dst'])
time.sleep(1)
import urllib.request
import urllib.parse
import json
import time
while True:
url = 'http://fanyi.baidu.com/v2transapi'
words = input("输入你想翻译的内容:")
data = {}
data['from'] = 'auto'
data['to'] = 'auto'
data['query'] = words
data['transtype'] = 'translang'
data['simple_means_flag'] = '3'
data = urllib.parse.urlencode(data).encode('utf-8')
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36'
}
req = urllib.request.Request(url, data, headers)
req.add_header('Referer', 'http://fanyi.baidu.com/')
response = urllib.request.urlopen(req)
html = response.read().decode('utf-8')
js = json.loads(html)
# print(html)
# print(js)
print(js['trans_result']['data'][0]['dst'])
time.sleep(1)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询