Python服务器在超时后从iOS接收POST请求数据(Python server receives POST request data from iOS after it has timed out

编程入门 行业动态 更新时间:2024-10-27 14:33:22
Python服务器在超时后从iOS接收POST请求数据(Python server receives POST request data from iOS after it has timed out)

我有一个iOS应用程序,可以发出POST请求和上传这样的文件。 另一方面,我有一个等待的Python服务器。

我面临的问题是,当iOS应用程序到达它想要发送请求的部分时,它会停止。 据我所知,请求发送方法的某些部分可以工作,因为服务器接收POST调用,但随后它停在那里。 在请求超时或我停止执行应用程序之前,它不会继续。 之后,服务器正常进行,奇怪的是它以某种方式得到了所有数据并可以按设计进行。 在模拟器和真实设备中,同步和异步都是一样的。

这是Objective-C处理请求的部分

// Get the image before we start building the request UIImage *image = [[PageViewControllerData sharedInstance] photoAtIndex:self.startingIndex]; NSData *temp = UIImageJPEGRepresentation(image, 0.8); // The request with URL NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"address-to-the-server"]]; request.HTTPMethod = @"POST"; // Boundary generation NSString *boundary = @"----block-end"; // Data size //NSString *postLength = [NSString stringWithFormat:@"%d", [temp length]]; //NSLog(@"%@", postLength); // Content type NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; //[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; // not necessary as it's done automatically [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; // Request's body building NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // Attach the body to the request [request setHTTPBody:body]; NSLog(@"Sending data."); // Create request parameters NSURLResponse *response = nil; NSError *error = nil; // and fire the request NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

这是python服务器代码的一部分:

def do_POST(self): print("POST method called") form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ= {'REQUEST_METHOD':'POST','CONTENT_TYPE':self.headers['Content-Type'],}) print("Form read") ....

我正在使用的设备是iPhone 4(7.1.2)。 在模拟器上我尝试过iPhone 4s和iPhone 6.服务器位于VirtualBox的Ubuntu环境中。

在服务器端,显示第一个打印消息,第二个打印消息不显示。 在请求超时或我停止程序执行后出现第二个。

如果你们中的任何一个人在考虑内容长度问题,那么可能不是这样,因为如果我不这样做,它会自动应用。 是的,我尝试自己应用它,它什么都没改变。

服务器已经使用常规浏览器进行了无数次测试,因为它能够分发一个特殊的表单,用户可以上传图片并且工作正常。

什么想法可能会在iOS上保留请求?

I have an iOS application that can make POST requests and upload files like this. On the the other side I have a Python server waiting.

The problem I'm facing is that when iOS application gets to the part where it's suppose to send the request, it stalls. From what I can tell, some part of the request sending method works, because the server receives the POST call, but then it stalls there. It won't proceed until the request has either timed out or I stop the execution of the application. After that the server proceeds as normal and the weird thing is that it has somehow gotten all the data and can proceed as designed. It's the same with both synchronous and asynchronous, both in simulator and real device.

Here's the part of Objective-C that deals with the request

// Get the image before we start building the request UIImage *image = [[PageViewControllerData sharedInstance] photoAtIndex:self.startingIndex]; NSData *temp = UIImageJPEGRepresentation(image, 0.8); // The request with URL NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"address-to-the-server"]]; request.HTTPMethod = @"POST"; // Boundary generation NSString *boundary = @"----block-end"; // Data size //NSString *postLength = [NSString stringWithFormat:@"%d", [temp length]]; //NSLog(@"%@", postLength); // Content type NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; //[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; // not necessary as it's done automatically [request addValue:contentType forHTTPHeaderField:@"Content-Type"]; // Request's body building NSMutableData *body = [NSMutableData data]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // Attach the body to the request [request setHTTPBody:body]; NSLog(@"Sending data."); // Create request parameters NSURLResponse *response = nil; NSError *error = nil; // and fire the request NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Here's part of the python server code:

def do_POST(self): print("POST method called") form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,environ= {'REQUEST_METHOD':'POST','CONTENT_TYPE':self.headers['Content-Type'],}) print("Form read") ....

The device I'm using is iPhone 4 (7.1.2). On simulator I have tried iPhone 4s and iPhone 6. Server is sitting in Ubuntu environment in VirtualBox.

On server side, the first print message is shown, the second one is not. The second one appears after the request has either timed out or I stop the program execution.

If any of you were thinking about Content-Length problem then it probably is not that, because it gets applied automatically if I don't. And yes, I tried applying it myself, it changed nothing.

Server has been tested countless times with a regular browser, since it's capable of handing out a special form where one can upload pictures and it is working perfectly.

Any ideas what might be keeping the request on iOS?

最满意答案

好的,所以终于解决了。

显然,最后一个边界线必须在边界字符串之后有一个额外的“ - ”,以向服务器发出信号,表明所有数据都已发送,并且没有其他内容。

基本上这个:

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

必须是这样的

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

(注意最后一行在边界字符串后面有两个额外的“ - ”。

这样做的时候我跟着多个例子而且没有人这样做。 这是一个示例指南 。 stackoverflow.com上有很多例子,至少我碰巧看到的都是这个。 我猜想随着时间的推移会发生一些变化(看到该指南的日期),现在这已成为必需的部分。

感谢我的课程伙伴猜到了这个问题。

Okay, so finally got it fixed.

Apparently the very last boundary line must have an additional "--" after the boundary string to signal the server that all the data has been sent and there is nothing more coming.

Basically this:

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

has to be like this

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"temp_target.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:temp]]; [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

(notice the very last line has two extra "--" right after the boundary string.

I was following multiple examples when doing this and none of them had that. Here's an example guide. There are many examples here on stackoverflow.com as well and at least the ones I happened to see were all missing this. I guess something has changed over time (seeing the date of that guide), making this now a required part.

Thanks to my course mate that guessed the issue.

更多推荐

本文发布于:2023-08-05 08:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1428762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:服务器   数据   POST   iOS   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!