【问题标题】:EPIPE(Broken Pipe) Error while converting Python client code to java(android)将 Python 客户端代码转换为 java(android) 时出现 EPIPE(Broken Pipe) 错误
【发布时间】:2017-08-02 05:27:02
【问题描述】:

我打算用Tensorflow 做一个分类器,所以我使用flaskrequests 库将文件从客户端发送到运行我的分类器的烧瓶服务器。

flask服务器的代码是:

# Create flask app
app = Flask(__name__)
# Set upload floder
app.config['UPLOAD_FOLDER'] = './upload'
# Max content length
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024

app.run(host='0.0.0.0')

@app.route('/upload', methods=['POST'])
def upload_file():
    # Check file upload field
    if 'file' not in request.files:
        return ''
    file = request.files['file']
    # Return if file does not have name
    if file.filename == '':
        return ''
    # Save in upload folder
    audio_file = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
    file.save(audio_file)

python客户端的代码是:

import requests 

url = 'http://www.testurl.com/upload'

files = {'file': open('sound/sample/sample/carhorn/1.wav', 'rb')}
r = requests.post(url, files=files)
print(r.text)

我想将flask服务器与Android设备连接起来,所以我将这个Python客户端代码重写为java。我使用 'okhttp 3.5.0' 库来发送多部分/表单数据。

java客户端的代码是:

public void uploadFile(String filepath) throws CustomException {
    String requestURL = "http://www.testurl.com/upload";
    File file = new File(filepath);
    OkHttpClient client = new OkHttpClient();
    RequestBody requestBody = new MultipartBody.Builder()
            .setType(MultipartBody.FORM)
            .addFormDataPart("file", file.getName(),
                    RequestBody.create(MediaType.parse("audio/wav"), file))
            .build();

    Request request = new Request.Builder()
            .url(requestURL)
            .post(requestBody)
            .build();

    Response response = null;

    try {
        response = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (response == null || !response.isSuccessful()) {
        Log.w("Server", "Unable to upload to server.");
    } else {
        Log.v("Server", "Upload was successful.");
    }
}

但是使用此代码,它会产生此“EPIPE”错误:

03-12 00:01:59.434 29084-29756/com.ishs.fylproject.classifierclient W/System.err: java.net.SocketException: sendto failed: EPIPE (Broken pipe)
    03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:542)
    03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:511)
    03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:500)
    03-12 00:01:59.436 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl.-wrap1(PlainSocketImpl.java)
    03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
    03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.Okio$1.write(Okio.java:78)
    03-12 00:01:59.437 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
    03-12 00:01:59.439 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
    03-12 00:01:59.439 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.write(RealBufferedSink.java:41)
    03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:287)
    03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
    03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.writeAll(RealBufferedSink.java:99)
    03-12 00:01:59.440 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RequestBody$3.writeTo(RequestBody.java:118)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:48)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    03-12 00:01:59.441 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    03-12 00:01:59.442 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    03-12 00:01:59.443 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
    03-12 00:01:59.444 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    03-12 00:01:59.445 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    03-12 00:01:59.445 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
    03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RealCall.execute(RealCall.java:63)
    03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at com.ishs.fylproject.classifierclient.SoundAlert.uploadFile(SoundAlert.java:249)
    03-12 00:01:59.446 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at com.ishs.fylproject.classifierclient.SoundAlert.copyWaveFile(SoundAlert.java:298)
    03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at com.ishs.fylproject.classifierclient.SoundAlert.stopRecording(SoundAlert.java:212)
    03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at com.ishs.fylproject.classifierclient.SoundAlert.access$400(SoundAlert.java:66)
    03-12 00:01:59.447 29084-29756/com.ishs.fylproject.classifierclient W/System.err:     at com.ishs.fylproject.classifierclient.SoundAlert$ExampleThread.run(SoundAlert.java:386)

有没有办法解决这个错误? 或者有什么方法可以像我写的 Python 客户端代码一样在 android 上发送 multipart/form 数据?

谢谢,

已编辑

我将java客户端代码中的response = client.newCall(request).execute();编辑为

client.newCall(request).enqueue(new Callback() {
    @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

     @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            Headers responseHeaders = response.headers();
            for (int i = 0, size = responseHeaders.size(); i < size; i++) {
                System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
            }

            System.out.println(response.body().string());
        }
    }
    );
} catch (Exception e) {
    e.printStackTrace();
}

但仍然出现此错误:

03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err: java.net.SocketException: sendto failed: EPIPE (Broken pipe)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:546)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at libcore.io.IoBridge.sendto(IoBridge.java:515)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl.write(PlainSocketImpl.java:504)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:37)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:266)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.Okio$1.write(Okio.java:78)
03-12 22:05:59.565 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.AsyncTimeout$1.write(AsyncTimeout.java:179)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.write(RealBufferedSink.java:41)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http1.Http1Codec$FixedLengthSink.write(Http1Codec.java:287)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okio.RealBufferedSink.writeAll(RealBufferedSink.java:99)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RequestBody$3.writeTo(RequestBody.java:118)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.MultipartBody.writeTo(MultipartBody.java:113)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:48)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-12 22:05:59.575 26156-26525/com.ishs.fylproject.classifierclient W/System.err:     at java.lang.Thread.run(Thread.java:818)

【问题讨论】:

  • 这段代码是否在 AsyncTask 中? execute() 的 OkHttp 无法在 UI 线程上运行
  • @cricket_007 哦..这是问题所在吗?我会试试看。非常感谢。
  • 现在试试client.newCall(request).enqueue(...
  • @cricket_007 还是不行..
  • 我将response = client.newCall(request).execute(); 更改为client.newCall(request).enqueue(new Callback() {...,但仍然出现错误“EPIPE - Broken Pipe”..

标签: java android python http python-requests


【解决方案1】:

当您的烧瓶代码过早返回时,您的管道会损坏。

app.run(host='0.0.0.0')  # App is running here with no routes

@app.route('/upload', methods=['POST'])
def upload_file():
    # Check file upload field
    if 'file' not in request.files:
        return ''
    file = request.files['file']
    # Return if file does not have name
    if file.filename == '':
        return ''

return '' 或函数末尾都返回 None 且没有明确的返回类型,因此 OkHttp 无法真正知道 response.isSuccessful() 是否为真,尽管空字符串可能是 200 响应代码在烧瓶中。

如果您可以让 Flask 返回具有有意义消息的实际 Response 对象会更好,以便您可以更好地调试错误。


其次,您需要将app.run() 语句移动到所有路由的末尾,否则设置不正确。

Flask tutorial - 404 Not Found


不过,我不完全确定您是否需要多部分请求。

您可以直接发布文件。见https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostFile.java

【讨论】:

  • 我觉得我的python服务器有问题。当我删除 app.run(host='0.0.0.0') 并从 localhost 发送时,它工作得很好,但是当我输入该代码(app.run)时,它使 ('Connection aborted.', error(32, 'Broken pipe')) 这个错误。
  • 您需要0.0.0.0 才能使服务器全局可见。您的 Android 应用不会发送到 localhost,因为那将是 Android 设备本身
  • 哦,终于解决了。我将代码 app.run(host='0.0.0.0') 移动到我的 python 代码的最后一行,它工作了! stackoverflow.com/questions/25383187/… 这帮助我移动了代码 app.run(host='0.0.0.0')。感谢您在我的问题上添加 cmets!
  • 哦,是的。完全阅读...但我是正确的。您的代码“过早地”运行
猜你喜欢
  • 1970-01-01
  • 2012-06-14
  • 2013-01-15
  • 2011-03-27
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多