检查POST

时间:2015-06-23 05:24:23

标签: php python ios http-post server

我正在尝试检查POST中是否定义了键和值,我想检查param是否存在。我在iOS中这样称呼它,而不是我发布帖子真的很重要

NSString *param = @"hello world";
NSString *postString = [NSString stringWithFormat:@"param=%@",param];
NSString *urlString = @"URL";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postString length]] forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
//...

然后我有一个PHP脚本返回与param相同的值,但是现在我想在Python中做同样的事情,所以我将转到我的python脚本所在的url然后它将检查那里是param的值,如果返回相同的值。那我怎么能在python中做到这一点?

如果你需要,这是php脚本

<?php
$response=array();
if(isset($_POST['param'])){
  $response['success'] = true;
  $response['message'] = 'received param = '.$_POST['param'];
}else{
  $response['success'] = false;
  $response['message'] = 'did not receive param';
}
$json = json_enconde($response);

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

以下是纯Python。我建议你去Django在Web领域使用Python。

import cgi
import json

form = cgi.FieldStorage()

param = form.getvalue("param")

if param:
    json.dumps({'success': True, 'message':  param })
else:
    json.dumps({'success': False, 'message':  'did not receive parameter' })