2015-03-13 86 views
0

我有一個龍捲風服務器,充當iPhone應用的REST接口。我想使用python程序對所述服務器執行一些查詢。在iOS端,我使用的是AFNetworking庫。但我不知道python客戶端的模擬器是什麼。如何通過python3通過SSL進行BasicAuth REST查詢

我的ObjectiveC/AFNetworking代碼看起來是這樣的:

+ (HttpConnection*) current { 
    static HttpConnection* current = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     current = [[self alloc] initWithBaseURL: [NSURL URLWithString: BaseURL]]; 
     current.securityPolicy.allowInvalidCertificates = YES; 
     current.responseSerializer = [AFJSONResponseSerializer serializer]; 
     current.requestSerializer = [AFJSONRequestSerializer serializer]; 
    }); 
    [current.requestSerializer setAuthorizationHeaderFieldWithUsername: Username password: Password]; 
    return current; 
} 

- (void)pull: (NSString*)resource fields: (NSArray*)fields then: (void(^)())block { 
    // NSLog(@"%@ pulling...", self); 
    NSString* uri = [NSString stringWithFormat: @"%@/%@", [self rootREST], self.serialID]; 
    if (resource) { 
     uri = [NSString stringWithFormat: @"%@/%@", uri, resource]; 
    } 
    [[HttpConnection current] 
     GET: uri 
     parameters: fields ? @{@"fields": fields} : nil 
     success:^(NSURLSessionDataTask* task, id responseObject){ 
      [self performSelectorOnMainThread: @selector(fromDoc:) withObject:responseObject waitUntilDone:YES]; 
      if (block) block(); 
     } 
     failure:^(NSURLSessionDataTask* task, NSError* error){ 
      NSLog(@"%@ pull error: %@ (uri=%@)", self, error, uri); 
     } 
    ]; 
} 

所以我要找的特點是:

  1. 初始化像https://a.b.c:7895/
  2. 一個的BaseURL添加resource/path
  3. 允許無效(自簽名)證書
  4. 設置BasicAuth
  5. 添加附加參數查詢
  6. 運行GET請求

難道我用urllib做到這一切的?或者我需要使用不同的東西。我會喜歡我如何做到這一點的例子。

回答

1

我會強烈建議python請求庫(我沒有檢查,它確實支持python 3.x)。它應該是如此簡單:

import requests 
username = "SomeUser" 
password = "SomePass" 
r = requests.get("https://a.b.c:7895/resource/path?param1=foo",auth=(username,password),verify=False) 
+0

看起來好像還有一個debian軟件包。我會盡力的,謝謝。 – 2015-03-13 19:15:43