我使用以下Ruby代码段下载8.9MB文件.
require 'open-uri'
require 'net/http'
require 'uri'
def http_download_no_progress_bar(uri, filename)
uri.open(read_timeout: 500) do |file|
open filename, 'w' do |io|
file.each_line do |line|
io.write line
end
end
end
end
Run Code Online (Sandbox Code Playgroud)
我想添加progressbar gem来可视化下载过程:
require 'open-uri'
require 'progressbar'
require 'net/http'
require 'uri'
def http_download_with_progressbar(uri, filename)
progressbar = nil
uri.open(
read_timeout: 500,
content_length_proc: lambda { |total|
if total && 0 < total.to_i
progressbar = ProgressBar.new("...", total)
progressbar.file_transfer_mode
end
},
progress_proc: lambda { |step|
progressbar.set step if progressbar
}
) do |file|
open …Run Code Online (Sandbox Code Playgroud) 我正在为要连接的几个不同客户端程序实现一个 API,我希望 API 能够发回“您确定要这样做吗?” 消息,例如尝试通过 api 删除某些内容时。查看已建立的 http 响应代码列表,我没有看到一个似乎很合适的代码。我总是可以跳过使用代码并提供一些其他方式来传达确认,但如果可能的话,我真的很想使用标准化的方法。
api 通常是通过客户端应用程序访问的,目前是一个由 JavaScript 驱动的应用程序,但我也做了它,以便可以通过网络浏览器 url 直接访问 api 来有效地使用它。
i', 编写 mitmproxy 代理 ( http://mitmproxy.org/index.html ) 以根据其 IP 将 HTTP 和 HTTPS 请求和响应写入文件(然后每个客户端可以访问它自己的请求\响应)以进行单元测试移动的。
就我现在所见,我不能像在 fiddler 中一样使用 str(Flow.request) 或 repr(Flow.request) 来获取响应\请求的“原始”打印,我需要重建它来自 Request 和 Response 对象的内部数据。
有人知道更好的方法吗?我正在使用 :
def response(ScriptContext, Flow):
Flow.request....
Flow.response....
Run Code Online (Sandbox Code Playgroud)
要访问被拦截的请求或响应,我没有改变任何东西,只是观察。现在代理在 8080 上,稍后它是 80 和 443 上的透明代理。如果有人之前做过,我会很高兴你能分享一些信息。
在特定情况下,我想以text/html内容类型响应错误,如下所示:
class MyResource(Resource):
def get(self):
if some_condition:
return 'bad argument', 400
Run Code Online (Sandbox Code Playgroud)
上面的代码返回一个application/json内容类型:'"bad argument"'
而不是一个text/html内容类型:'bad argument'
如何强制flask-restful 响应text/html内容类型?
我正在视图中生成报告下载并在处理 POST 数据后开始下载。这意味着用户发送表单并开始下载:
视图.py
def export(request):
if request.method == 'POST' and 'export_treat' in request.POST:
form1 = TransExport(request.POST, instance= obj)
if form1.is_valid():
...
...
response=HttpResponse(ds.xls,content_type="application/xls")
response['Content-Disposition'] = 'attachment; filename="Report_Behandlungen.xls"'
return response
Run Code Online (Sandbox Code Playgroud)
我需要的是下载(或重定向)后的页面刷新。我怎样才能做到这一点?
嗨,我正在使用客户端 Http (apache) 和 json-simple。
我想访问json响应的属性,然后使用它们。
知道如何做到这一点吗?我读了一篇文章,但我并没有像它那样工作。
这是我的回答 json:
{"Name":"myname","Lastname":"mylastname","Age":19}
Run Code Online (Sandbox Code Playgroud)
这是我的代码java:
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(
"http://localhost:8000/responsejava");
getRequest.addHeader("accept", "application/json");
HttpResponse response = httpClient.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader(
(response.getEntity().getContent())
)
);
StringBuilder content = new StringBuilder();
String line;
while (null != (line = br.readLine())) {
content.append(line);
}
Object obj=JSONValue.parse(content.toString());
JSONObject finalResult=(JSONObject)obj;
System.out.println(finalResult);
httpClient.getConnectionManager().shutdown();
Run Code Online (Sandbox Code Playgroud)
我打印了 null,我做错了什么?
我们正在实施我们自己的分析,因为我们公开了一个需要调用的 Web 服务,该服务将捕获我们数据库中的数据。
问题是,由于这是分析,我们将进行大量调用(例如每个页面加载、每个 js 之后调用、CSS 加载等),因此会有很多这样的调用。所以我不希望服务器加载大量请求,以便更精确地等待响应。因为我们得到的回应对我们几乎没有任何用处。
那么有什么方法可以只触发 Web 服务请求而忘记我已经触发了它?
我知道每个 HTTP 请求也会有响应。
因此,让我想到的一件事是,如果我们将请求超时设置为零秒会怎样?但我不确定这是否是正确的做法。
请为我提供更多建议
我只想知道可以在浏览器(如 Chrome、Firefox)中查看哪些文件类型(如 .png、.pdf、.txt)。因为有时当我点击文件链接时,它会自动下载而不是在浏览器中显示。为什么会发生这种情况?下面是我的代码。任何帮助将不胜感激。
<script>
function openPDF(url){
var w=window.open(url, '_blank');
w.focus();
}
</script>
<a href="burger1.jpg" target='_blank'>link</a>
<div onclick="openPDF('1.pdf');">PDF 1</div>
Run Code Online (Sandbox Code Playgroud) 如果我浏览到https://httpbin.org/headers我希望得到以下 JSON 响应:
{
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Connection": "close",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用硒
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
url = 'https://httpbin.org/headers'
driver.get(url)
print(driver.page_source)
driver.close()
Run Code Online (Sandbox Code Playgroud)
我得到
<html platform="linux" class="theme-light" dir="ltr"><head><meta http-equiv="Content-Security-Policy" content="default-src 'none' ; script-src resource:; "><link rel="stylesheet" type="text/css" href="resource://devtools-client-jsonview/css/main.css"><script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="viewer-config" src="resource://devtools-client-jsonview/viewer-config.js"></script><script type="text/javascript" charset="utf-8" …Run Code Online (Sandbox Code Playgroud) 我有一个简单的反应组件:
import React from "react"
import axios from "axios"
export default () => {
const axios = require("axios")
axios.get("http://localhost:8000/user").then(function(res) {
// handle success
console.log(res)
})
return (
<div>
Hello
</div>
)
}
Run Code Online (Sandbox Code Playgroud)
我确实得到了一些东西,状态代码为 200,并且它执行了 console.log,但数据字段为空。这只是data: ""; 我发出的 get 请求是针对我自己的 nodejs 服务器。我应该找回当前登录的用户。这在我直接访问 URL 时有效 ( localhost:8000/user)。看起来像这样
"displayName": "thomas",
"id": "google-oauth2|197913719739127391723",
"user_id": "google-oauth2|197913719739127391723",
"name": {
"givenName": "thomas3000"
},
"emails": [
{
"value": "thomas3k@tmail.com"
}
],
"picture": "https://akfaf/afaf/photo.jpg",
"locale": "en",
"nickname": "thomas3000",
"_json": {
"sub": "google-oauth2|197913719739127391723",
"given_name": "thomas3k",
"nickname": "tom", …Run Code Online (Sandbox Code Playgroud) httpresponse ×10
http ×4
httprequest ×2
java ×2
json ×2
python ×2
analytics ×1
axios ×1
browser ×1
buffer ×1
content-type ×1
django ×1
download ×1
html ×1
http-headers ×1
httpclient ×1
javascript ×1
json-simple ×1
mitmproxy ×1
node.js ×1
open-uri ×1
progress-bar ×1
python-2.7 ×1
redirect ×1
refresh ×1
ruby ×1
selenium ×1