2017-03-06 76 views
0

我想測試Shodan數據。數據包括時間戳,抓取工具ID,服務器操作系統等字段。這些內容隨每個請求而變化。我應該測試它們嗎?UnitTesting Shodan data

初段JSON數據:

{ 
    "city": "Mountain View", 
    "region_code": "CA", 
    "os": null, 
    "tags": [], 
    "ip": 134744072, 
    "isp": "Google", 
    "area_code": 650, 
    "dma_code": 807, 
    "last_update": "2017-03-04T13:54:57.176297", 
    "country_code3": "USA", 
    "country_name": "United States", 
    "hostnames": [ 
     "google-public-dns-a.google.com" 
    ], 
    "postal_code": "94035", 
    "longitude": -122.0838, 
    "country_code": "US", 
    "ip_str": "8.8.8.8", 
    "latitude": 37.385999999999996, 
    "org": "Google", 
    "data": [ 
     { 
      "_shodan": { 
       "options": {}, 
       "id": null, 
       "module": "dns-udp", 
       "crawler": "122dd688b363c3b45b0e7582622da1e725444808" 
      }, 
      "hash": -553166942, 
      "os": null, 
      "opts": {}, 
      "ip": 134744072, 
      "isp": "Google", 
      "port": 53, 
      "hostnames": [ 
       "google-public-dns-a.google.com" 
      ], 
      "location": { 
       "city": "Mountain View", 
       "region_code": "CA", 
       "area_code": 650, 
       "longitude": -122.0838, 
       "country_code3": "USA", 
       "country_name": "United States", 
       "postal_code": "94035", 
       "dma_code": 807, 
       "country_code": "US", 
       "latitude": 37.385999999999996 
      }, 
      "timestamp": "2017-03-04T13:54:57.176297", 
      "domains": [ 
       "google.com" 
      ], 
      "org": "Google", 
      "data": "\nRecursion: enabled", 
      "asn": "AS15169", 
      "transport": "udp", 
      "ip_str": "8.8.8.8" 
     } 
    ], 
    "asn": "AS15169", 
    "ports": [ 
     53 
    ] 
} 

我的測試文件:

def test_shodan_api(): 
    assert shodan_data == ??? 
+0

盪滌語法和更新標籤。更好的描述會有所幫助! – AlG

回答

0

我想你想你的實際接收到的數據與存儲信息相比較,並絆倒的事實,某些部分(時間戳)在每次通話中有所不同,因此您的完整數據永遠不會與準確的數據相匹配。

我建議從兩個罐頭的數據和接收數據刪除時間戳和比較其他地區:

del received_data['last_update'] 
del canned_data['last_update'] # you probably want to do this prior to canning the data ;-) 

assert_equal(received_data, canned_data) 
+0

恩,謝謝你的時間!我認爲,我想知道是否有不同的解決方案。 – nigella

+0

當然,有很多可用的選項。雖然這與黑名單類似(可以刪除不需要的部分),但您也可以使用白名單:僅比較一些特定值:'['...','...',...]中的鍵:assert_equal(received_data [key],canned_data [key])' – Alfe

+0

順便說一下,既然你在這裏看起來很新,歡迎來到StackOverflow!如果你發現一個有價值的答案,你可以自由選擇它(答案左上角的三角形)。如果您認爲答案可以解決您的問題,請隨時接受答案(答案左邊的灰色複選標記)。如果一個答案沒有幫助,隨意降低它(下三角)。具體的「謝謝」評論通常不是必要的,通常不鼓勵將帖子保持在較小的範圍內。 – Alfe

相關問題