How to access Nested Dictionary values through index - python

Im building a scanner using python-nmap libary. Here's the code :
import nmap
import json
def Nmap_Recon(host, port):
nm = nmap.PortScanner()
lol = nm.scan(host, '22-443')
print(lol['scan'])
Nmap_Recon('www.stuxnoid.org',80)
Output :
{'77.72.0.90': {'hostnames': [{'name': 'www.stuxnoid.org', 'type': 'user'}, {'name': 'carbon.cloudhosting.co.uk', 'type': 'PTR'}], 'addresses': {'ipv4': '77.72.0.90'}, 'vendor': {}, 'status': {'state': 'up', 'reason': 'syn-ack'}, 'tcp': {25: {'state': 'open', 'reason': 'syn-ack', 'name': 'smtp', 'product': '', 'version': '', 'extrainfo': '', 'conf': '3', 'cpe': ''}, 80: {'state': 'open', 'reason': 'syn-ack', 'name': 'http', 'product': 'imunify360-webshield/1.6', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': ''}, 443: {'state': 'open', 'reason': 'syn-ack', 'name': 'https', 'product': 'imunify360-webshield/1.6', 'version': '', 'extrainfo': '', 'conf': '10', 'cpe': ''}}}}
I guess the output is in dictionary format. The problem is, I want to display only the open port details. But the port details are nested inside the dict_key IP address (77.72.0.90) and It keeps changing with the domain I pass. How to access those Open port details and display them?

if your question is how to get first item out of a dictionary (arbitrary item prior to python 3.6) it can be done so:
next(iter(lol['scan'].values()))
or, destructively (this will return last item):
lol['scan'].popitem()[1]

Related

JIRA JQL Query - Results

I'm writing a JQL query to fetch only Service Requests. I'm not able to fetch the Service Request field name from the below json using JQL Query. Any help will be appreciated
'jql': 'key=ITSM-1917' => This is working .. But I'm trying to fetch based on issuetype.name='Service Request'
{'expand': 'names,schema', 'startAt': 0, 'maxResults': 100, 'total': 1, 'issues': [{'expand': 'operations,versionedRepresentations,editmeta,changelog,renderedFields', 'id': '373234', 'self': '', 'key': 'ITSM-1917', 'fields': {'issuetype': {'self': '', 'id': '10300', 'description': 'Created by JIRA Service Desk.', 'iconUrl': '', '**name': 'Service Request**', 'subtask': False, 'avatarId': 11006}, 'assignee': {'self': '', 'name': 'IT Service Management', 'key': 'JIRAUSER10945', 'emailAddress': '', 'avatarUrls': {'48x48': 'https://www.gravatar.com/avatar/067a17d84b041546f0f658bd011bc3ba?d=mm&s=48', '24x24': 'https://www.gravatar.com/avatar/067a17d84b041546f0f658bd011bc3ba?d=mm&s=24', '16x16': 'https://www.gravatar.com/avatar/067a17d84b041546f0f658bd011bc3ba?d=mm&s=16', '32x32': 'https://www.gravatar.com/avatar/067a17d84b041546f0f658bd011bc3ba?d=mm&s=32'}, 'displayName': 'IT Service Management', 'active': True, 'timeZone': 'America/New_York'}, 'created': '2022-11-23T01:34:11.000-0500', 'status': {'self': '', 'description': 'A resolution has been taken, and it is awaiting verification by reporter. From here issues are either reopened, certified, or are closed.', 'iconUrl': '', 'name': 'Resolved', 'id': '5', 'statusCategory': {'self': '', 'id': 3, 'key': 'done', 'colorName': 'green', 'name': 'Done'}}}}]}
You have to use the following JQL instead:
issuetype = "Service Request"
Please check the Jira search documentation. If you meant anything else, be more specific.

get a part from a dictionary

i'm trying to get the pulse as an output for the given url using this code
from OTXv2 import OTXv2
from OTXv2 import IndicatorTypes
otx = OTXv2("my_key")
test=otx.get_indicator_details_full(IndicatorTypes.DOMAIN, "google.com")
and when i print test i become this output:
{'general': {'sections': ['general', 'geo', 'url_list', 'passive_dns', 'malware', 'whois', 'http_scans'], 'whois': 'http://whois.domaintools.com/google.com', 'alexa': 'http://www.alexa.com/siteinfo/google.com', 'indicator': 'google.com', 'type': 'domain', 'type_title': 'Domain', 'validation': [{'source': 'ad_network', 'message': 'Whitelisted ad network domain www-google-analytics.l.google.com', 'name': 'Whitelisted ad network domain'}, {'source': 'akamai', 'message': 'Akamai rank: #3', 'name': 'Akamai Popular Domain'}, {'source': 'alexa', 'message': 'Alexa rank: #1', 'name': 'Listed on Alexa'}, {'source': 'false_positive', 'message': 'Known False Positive', 'name': 'Known False Positive'}, {'source': 'majestic', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}, {'source': 'whitelist', 'message': 'Whitelisted domain google.com', 'name': 'Whitelisted domain'}], 'base_indicator': {'id': 12915, 'indicator': 'google.com', 'type': 'domain', 'title': '', 'description': '', 'content': '', 'access_type': 'public', 'access_reason': ''}, 'pulse_info': {'count': 0, 'pulses': [], 'references': [], 'related': {'alienvault': {'adversary': [], 'malware_families': [], 'industries': []}, 'other': {'adversary': [], 'malware_families': [], 'industries': []}}}, 'false_positive':...
i want to get only the part 'count': 0 in pulse_info
i tried using test.values() but it's like i have many dictionaries together
any idea how can i solve that?
Thank you
print(test["general"]["pulse_info"]["count"])

DuckDuckGo API not responding when space in encoded url query

I kind of have two real questions. Both relate to this code:
import urllib
import requests
def query(q):
base_url = "https://api.duckduckgo.com/?q={}&format=json"
resp = requests.get(base_url.format(urllib.parse.quote(q)))
json = resp.json()
return json
One is this: When I query something like this: "US Presidents", I get back something like this:
{'Abstract': '', 'AbstractSource': '', 'AbstractText': '', 'AbstractURL': '', 'Answer': '', 'AnswerType': '', 'Definition': '', 'DefinitionSource': '', 'DefinitionURL': '', 'Entity': '', 'Heading': '', 'Image': '', 'ImageHeight': '', 'ImageIsLogo': '', 'ImageWidth': '', 'Infobox': '', 'Redirect': '', 'RelatedTopics': [], 'Results': [], 'Type': '', 'meta': {'attribution': None, 'blockgroup': None, 'created_date': '2021-03-24', 'description': 'testing', 'designer': None, 'dev_date': '2021-03-24', 'dev_milestone': 'development', 'developer': [{'name': 'zt', 'type': 'duck.co', 'url': 'https://duck.co/user/zt'}], 'example_query': '', 'id': 'just_another_test', 'is_stackexchange': 0, 'js_callback_name': 'another_test', 'live_date': None, 'maintainer': {'github': ''}, 'name': 'Just Another Test', 'perl_module': 'DDG::Lontail::AnotherTest', 'producer': None, 'production_state': 'offline', 'repo': 'fathead', 'signal_from': 'just_another_test', 'src_domain': 'how about there', 'src_id': None, 'src_name': 'hi there', 'src_options': {'directory': '', 'is_fanon': 0, 'is_mediawiki': 0, 'is_wikipedia': 0, 'language': '', 'min_abstract_length': None, 'skip_abstract': 0, 'skip_abstract_paren': 0, 'skip_icon': 0, 'skip_image_name': 0, 'skip_qr': '', 'src_info': '', 'src_skip': ''}, 'src_url': 'Hello there', 'status': None, 'tab': 'is this source', 'topic': [], 'unsafe': None}}
Basically, everything is empty. Even the Heading key, which I know was sent as "US Presidents" encoded into url form. This issue seems to affect all queries I send with a space in them. Even when I go to this url: "https://api.duckduckgo.com/?q=US%20Presidents&format=json&pretty=1" in a browser, all I get is a bunch of blank json keys.
My next question is this. When I send in something like this: "1+1", the json response's "Answer" key is this:
{'from': 'calculator', 'id': 'calculator', 'name': 'Calculator', 'result': '', 'signal': 'high', 'templates': {'group': 'base', 'options': {'content': 'DDH.calculator.content'}}}
Everything else seems to be correct, but the 'result' should be '2', should it not? The entire rest of the json seems to be correct, including all 'RelatedTopics'
Any help with this would be greatly appreciated.
Basically duckduckgo api is not a real search engine. It is just a dictionary. So try US%20President instead of US%20presidents and you'll get an answer. For encoding you can use blanks, but if it's not a fixed term I would prefer the plus-sign. You can do this by using urllib.parse.quote_plus()
With calculation you're right. But I see absolutely no use case to use a calculus-api within a python code. It is like using trampoline to travel to the moon if there is a rocket available. And maybe they see it the same and do not offer calc services in their api?!

How to convert a record or list into JSON format using python?

I am trying to convert a list into JSON using Python. The result from a query looks something like this :
Output:
[<Record r=<Relationship id=106 nodes=(<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>, <Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}>) type='subclass_of' properties={}> b=<Node id=59 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '93', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'A DataProcessor which represents a ML algorithm', 'sing': '', 'pl': '', 'title': 'LearningProcessor', 'version': 'v6.0'}> n=<Node id=71 labels=frozenset({'TBox'}) properties={'identifier': '', 'ontology_level': 'lower', 'neo4jImportId': '105', 'html_info': '', 'namespace': 'car', 'admin': '', 'description': 'remove', 'sing': '', 'pl': '', 'title': 'BMProcessor', 'version': 'v6.0'}>>]
Function :
def runQuery(query):
pprint.pprint(connection.execute(query))
When I perform a simple json.dumps() it return with TypeError: Object of type is not JSON serializable
I want to print a JSON format out of this. How can I do so?
You can get the result.data() which is a dictionary.
You can also iterate over the records in the cursor and convert one by one, and extract the fields you want, which is what the linked example above does - pass the cursor and use the blob['field'] syntax
def serialize_genre(genre):
return {
'id': genre['id'],
'name': genre['name'],
}

how to pull a specific node from a json data library, and return its value [duplicate]

This question already has answers here:
decoding json dictionary with python
(2 answers)
Closed 6 years ago.
i have a script that pulls json data from an api, and i want it to then after pulling said data, decode and pick which tags to store into a db. right now i just need to get the script to return specific called upon values. this is what the script looks like, before me trying to decode it.
import requests
def call():
payload = {'apikey':'945e8e8499474b7e8d2bc17d87191bce', 'zip' : '47120'}
bas_url = 'http://congress.api.sunlightfoundation.com/legislators/locate'
r = requests.get(bas_url, params = payload)
grab = r.json()
return grab
this is what i was told to try to get those specific tags returned
return [{key: value for key, value in result.items() if key in ("twitter_id", "office")} for result in jsonData]
but that returns errors. any suggestions?
'results': [{'twitter_id': 'RepToddYoung', 'ocd_id': 'ocd-division/country:us/state:in/cd:9', 'oc_email': 'Rep.Toddyoung#opencongress.org', 'middle_name': 'C.', 'votesmart_id': 120345, 'first_name': 'Todd', 'youtube_id': 'RepToddYoung', 'last_name': 'Young', 'bioguide_id': 'Y000064', 'district': 9, 'nickname': None, 'office': '1007 Longworth House Office Building', 'term_start': '2015-01-06', 'thomas_id': '02019', 'party': 'R', 'in_office': True, 'title': 'Rep', 'govtrack_id': '412428', 'crp_id': 'N00030670', 'term_end': '2017-01-03', 'chamber': 'house', 'state_name': 'Indiana', 'fax': '202-226-6866', 'phone': '202-225-5315', 'gender': 'M', 'fec_ids': ['H0IN09070'], 'state': 'IN', 'website': 'http://toddyoung.house.gov', 'name_suffix': None, 'icpsr_id': 21133, 'facebook_id': '186203844738421', 'contact_form': 'https://toddyoungforms.house.gov/give-me-your-opinion', 'birthday': '1972-08-24'}, {'twitter_id': 'SenDonnelly', 'ocd_id': 'ocd-division/country:us/state:in', 'oc_email': 'Sen.Donnelly#opencongress.org', 'middle_name': None, 'lis_id': 'S356', 'first_name': 'Joe', 'youtube_id': 'sendonnelly', 'last_name': 'Donnelly', 'bioguide_id': 'D000607', 'district': None, 'nickname': None, 'office': '720 Hart Senate Office Building', 'state_rank': 'junior', 'thomas_id': '01850', 'term_start': '2013-01-03', 'party': 'D', 'in_office': True, 'title': 'Sen', 'govtrack_id': '412205', 'crp_id': 'N00026586', 'term_end': '2019-01-03', 'chamber': 'senate', 'state_name': 'Indiana', 'fax': '202-225-6798', 'phone': '202-224-4814', 'gender': 'M', 'senate_class': 1, 'fec_ids': ['H4IN02101', 'S2IN00091'], 'state': 'IN', 'votesmart_id': 34212, 'website': 'http://www.donnelly.senate.gov', 'name_suffix': None, 'icpsr_id': 20717, 'facebook_id': '168059529893610', 'contact_form': 'http://www.donnelly.senate.gov/contact/email-joe', 'birthday': '1955-09-28'}, {'twitter_id': 'SenDanCoats', 'ocd_id': 'ocd-division/country:us/state:in', 'oc_email': 'Sen.Coats#opencongress.org', 'middle_name': 'Ray', 'lis_id': 'S212', 'first_name': 'Daniel', 'youtube_id': 'SenatorCoats', 'last_name': 'Coats', 'bioguide_id': 'C000542', 'district': None, 'nickname': None, 'office': '493 Russell Senate Office Building', 'state_rank': 'senior', 'thomas_id': '00209', 'term_start': '2011-01-05', 'party': 'R', 'in_office': True, 'title': 'Sen', 'govtrack_id': '402675', 'crp_id': 'N00003845', 'term_end': '2017-01-03', 'chamber': 'senate', 'state_name': 'Indiana', 'fax': '202-228-1820', 'phone': '202-224-5623', 'gender': 'M', 'senate_class': 3, 'fec_ids': ['S0IN00053'], 'state': 'IN', 'votesmart_id': 53291, 'website': 'http://www.coats.senate.gov', 'name_suffix': None, 'icpsr_id': 14806, 'facebook_id': '180671148633644', 'contact_form': 'http://www.coats.senate.gov/contact/', 'birthday': '1943-05-16'}]}
I'm guessing your json looks something like [{}, {}, {}] from what you posted. i.e. grab is a python list of dictionaries.
Try this?
get_nodes = lambda x : {"twitter_id" : x.get("twitter_id", None),
"office" : x.get("office", None)}
def call():
... whatever you had before ...
return map(get_nodes, grab)
Also, if you wanted your original answer to work, you need to add an "else" value. In the future, I would recommend posting what kind of error you're getting rather than just saying "this threw an error".

Categories

Resources