Hi All,
Newbie to Python
I ran a query from a MySql DB to get IP address and ping them and get reachable, congested, not reachable - say in the table I have column Dept IP ADDR
acct 1.1.2.3
HR 2.3.3.4
Mktng 3.4.4.5 - query gives me IP array and I ping from IP array and get 1.1.2.3 -reachable. 2.3.3.4 - congested and 3.4.4.5 - not reachable how can I re map using dictionary- the values in the Dept column to go with the right IPs and their results so I would get for e.g. HR 2.3.3.4 congested.
any help appreciated
--------------------------------------------------------------------------------
7 2113
I assume the result you receive from the query is an array for example results -
dict = {}
-
for result in result:
-
dict[result[0]] = result[1]
-
After that all the information will be stored in dict in the following matter: -
dict = {'acct':'<ip acct>', 'HR':'<ip hr>', ...}
-
Is that an answer to your question?
bvdet 2,851
Expert Mod 2GB
I moved this thread into the Answers forum.
BV
Moderator
micmast
This seems like the lead I am looking for , let me try it
TY .. will le u kno
bvdet 2,851
Expert Mod 2GB
Avoid using built-in Python function names for variable names such as dict. Assuming you were to receive ordered lists (arrays) for a department query and ip address query: - import os
-
-
# ip_array and dept_array are ordered lists from db query
-
ip_array = ['1.1.1.1', '2.2.2.2', '3.3.3.3']
-
dept_array = ['acct', 'hr', 'mktng']
-
-
dd = {}
-
-
for i, ip in enumerate(ip_array):
-
f = os.popen('ping %s' % (ip), 'r')
-
ping_result = [item.strip() for item in f.readlines() if item.strip()]
-
f.close()
-
dd[dept_array[i]] = (ip_array[i], ping_result)
-
-
for dept in dept_array:
-
print "%s: %s\n Results:" % (dept, dd[dept][0])
-
print ' '+'\n '.join(dd[dept][1])
bvdet
appreciate the reply - lots of good , new stuff
I am trying to disect your answer and understand every step but can you breifly explain 1. look like you are using i and ip 2 variables in your for loop to rep and loop thru each value in ip_array ?
and 2. dd[dept_array[i]] = (ip_array[i], ping_result) - are you assigning values ip_array[ i] and ping_result value to dd key dept_array[i]. ?
I would like to make the right comments so I can avoid asking this and similar questions in the future
Thx
bvdet 2,851
Expert Mod 2GB
dragrid,
1. enumerate(iterable) is a built-in Python function that returns an iterator object given an iterable object. The next() method of the iterator returns a tuple containing a count (similar to the range() function) and the corresponding item from the iterable. So the variables i and ip represent the count (starting at 0) and corresponding item from the iterable object ip_array.
2. Yes. Each the dictionary value is a list containing the ip address and result of the ping attempt for the corresponding ip address, and the key to the value is the department name.
-BV
BV - awsome stuff
appreciate all the reply
Thanks again for your help
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Yang Li Ke |
last post by:
Hi guys!
I have some datas that I must check
everytime a visitor comes to my site
What is better to do:
1- Read data from a file
or
2- Read data from a mysql db
Thank you
|
by: bissatch |
last post by:
Hi,
I have just started a new job as a webmaster where they use PostgreSQL.
I have always used MySQL.
What are the differences? How does PostgreSQL work with PHP as apposed
to MySQL? And...
|
by: Philip Stoev |
last post by:
Hi all,
Please tell me if any of this makes sense. Any pointers to relevant
projects/articles will be much appreciated.
Philip Stoev
http://www.stoev.org/pivot/manifest.htm
...
|
by: Ersin Basaran |
last post by:
I'm using MySQL 4.0.13 and having problem with the following query. It
was ok prior MySQL 4.
select substring(SomeField,1,1) as Letter from sometable group by
Letter
I use this query to get...
|
by: Michi |
last post by:
I was wondering what the best solution is for making large numbers of
TEXT (or BLOB?) fields searchable. For example, if I have a forum,
what is the best way to be able to search for specific...
|
by: news |
last post by:
We currently have our mySQL server on the same box as the Apache
server.
For security and load balancing, we're going to be moving the mySQL
server to another box.
We're already using a single...
|
by: Mairhtin O'Feannag |
last post by:
Hello,
I have a client (customer) who asked the question : "Why would I buy and
use UDB, when MySql is free?"
I had to say I was stunned. I have no experience with MySql, so I was
left sort...
|
by: Derick van Niekerk |
last post by:
I have found many posts that deal with writing a dictionary to MySQL in
a blob field - which I can't imagine why anybody would want to do it.
I want to write each element of a dictionary onto a...
|
by: Pom |
last post by:
Hello
I want to convert a Mysql resulset to a dictionary.
I made some code myself, and want to ask you if I do this the right way.
def remapmysql(a):
return (a, (a))
def test_map():
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |