Re: [Tutor] Custom Function that Takes argument

2015-10-31 Thread Nym City via Tutor
Hello,
Thank you for your response. I went back and updated encoding to utf-8 and 
ASCII but I still experienced issues with the output.
The part that is interesting is that the output for each of the following 
fields is the (should be) the same    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip

Search by zip comes out just fine but only the partial output is shared for the 
first two options. I don't understand why that would be? I would think if its 
the same output for all three options that I would either work for all three or 
not work for any. Also, why the partial results. 

Any suggestions on how to normalize this?

 Thank you. 


 On Wednesday, October 21, 2015 5:53 AM, Alan Gauld 
<alan.ga...@btinternet.com> wrote:
   
 

 On 21/10/15 01:18, Nym City via Tutor wrote:

> def zip_search(query):
>      api_key = locu_api
>      url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
>      zip = query.replace(' ', '%20')
>      final_url = url + '=' + zip + "=restaurant"
>      jason_obj = urllib2.urlopen(final_url)
>      data = json.load(jason_obj)
>      for item in data['objects']:
>          print item['name'], item['phone'], item['street_address'], 
>item['categories'], item['website_url']
>
> ans=True
> while ans:
>      print ("""
>      1.Search by City
>      2.Search by Region (State Abbreviation)
>      3.Search by Zip
>      4.Exit/Quit
>      """)
>      ans=raw_input("What would you like to do? ")
>      if ans=="1":
>        locality = raw_input("\nEnter City ")
>        print locu_search(locality)
>        break
>      elif ans=="2":
>          region = raw_input("\n Search by State ")
>          print region_search(region)
>          break
>      elif ans=="3":
>          zip = raw_input("\n Search by Zip ")
>          print zip_search(zip)
>          break
>      elif ans=="4":
>        print("\n Goodbye")
>        break

Because you now process the results in the if clauses you no
longer need the breaks. In fact if you want the menu to be
repeated you need to take them all out except for the last one.


> -
> I am not sure if above changes exactly reflect your suggestions but I tested 
> the logic and it seems to work.
>
> The only issue is that on the first two queries (locu_search(locality) and
> region_search(region)) I receive the following traceback error:
> Traceback (most recent call last):
>    File 
>"C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 
>47, in 
>      print locu_search(locality)
>    File 
>"C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", line 
>14, in locu_search
>      print item['name'], item['phone'], item['street_address']
>    File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
>      return codecs.charmap_encode(input,errors,encoding_table)
> UnicodeEncodeError: 'charmap' codec can't encode character u'\u200e' in 
> position 29: character maps to 

Looks like you are using cpl252 and your data is coming back as 
something else (UTF-8 maybe?) so you probably need to specify
the encoding.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Custom Function that Takes argument

2015-10-21 Thread Nym City via Tutor
Hello,
Thank you for your feedback. Sorry it took me a while to revise my code but 
here is my code with the changes:
import urllib2
import json

locu_api = 'redacted'

def locu_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    locality = query.replace(' ', '%20')
    final_url = url + '=' + locality + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address']

def region_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    region = query.replace(' ', '%20')
    final_url = url + '=' + region + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['locality'], item['website_url']

def zip_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    zip = query.replace(' ', '%20')
    final_url = url + '=' + zip + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['categories'], item['website_url']

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
  locality = raw_input("\nEnter City ")
  print locu_search(locality)
  break
    elif ans=="2":
    region = raw_input("\n Search by State ")
    print region_search(region)
    break
    elif ans=="3":
    zip = raw_input("\n Search by Zip ")
    print zip_search(zip)
    break
    elif ans=="4":
  print("\n Goodbye")
  break 
-
I am not sure if above changes exactly reflect your suggestions but I tested 
the logic and it seems to work. 

The only issue is that on the first two queries (locu_search(locality) and 
region_search(region)) I receive the following traceback error:
Traceback (most recent call last):
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 47, in 
    print locu_search(locality)
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 14, in locu_search
    print item['name'], item['phone'], item['street_address']
  File "C:\Python27\lib\encodings\cp1252.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u200e' in 
position 29: character maps to 

The last query about zip definition works perfectly. Please share thoughts on 
the revised logic and recommendation for handling the error above. Thanks!

Please
Thank you. 


 On Monday, October 12, 2015 8:30 PM, Alan Gauld 
 wrote:
   
 

 On 13/10/15 00:32, Nym City wrote:
>
> ans=raw_input("What would you like to do? ")
>    if ans=="1":
>      locality = raw_input("\nEnter City ")
>      break
>    elif ans=="2":
>        region = raw_input("\n Search by State ")
>        break
>    elif ans=="3":
>        zip = raw_input("\n Search by Zip ")
>        break
>    elif ans=="4":
>      print("\n Goodbye")
>      break
> ...  The use case is that by not knowing which of the 4 options the 
> user will select,


But you know what they select after they have done it so you need to wait
till that point to run the appropriate  query. And ans tells you which 
choice
the user made.

> NameError: name 'locality' is not defined
>
> 
> The error points back to where this is:
>
> varibles = [locu_search(locality), region_search(region), zip_search(zip)]
>
> I don't understand why Python thinks that locality (and others) are 
> not defined when I defined them at the very beginning.
>

You didn't. You only define them once the user makes a choice.
eg. If the ans is 3 then locality and region are not defined.

But even if they were (with default values) you don;t want to
run all the queries each time. Only run the query selected by the user.
So call the query functions in the if/else structure where you set the 
variables.


HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Custom Function that Takes argument

2015-10-12 Thread Nym City via Tutor
Thank you for your response. I updated the first portion of my code to include 
breaks: 

import urllib2
import json

locu_api = 'redacted'

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by Region (State Abbreviation)
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
  locality = raw_input("\nEnter City ")
  break
    elif ans=="2":
    region = raw_input("\n Search by State ")
    break
    elif ans=="3":
    zip = raw_input("\n Search by Zip ")
    break
    elif ans=="4":
  print("\n Goodbye")
  break

def locu_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    locality = query.replace(' ', '%20')
    final_url = url + '=' + locality + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address']


def region_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    region = query.replace(' ', '%20')
    final_url = url + '=' + region + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['locality'], item['website_url']

def zip_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    zip = query.replace(' ', '%20')
    final_url = url + '=' + zip + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['categories'], item['website_url']

def non_empty_variable(varibles):
    for varible in varibles:
    if varible != "":
    print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]


-
With the above updates, the user prompts are working fine now. Thanks!

The issue that I am trying to solve now is print the final result.  The use 
case is that by not knowing which of the 4 options the user will select, i need 
a way to figure out which selection user made, run it against the appropriate 
query and than print out the result. 
My thought process is to narrow down the user selection by figuring out which 
of the 4 choices is not blank. To do this I created this:
def non_empty_variable(varibles):
    for varible in varibles:
    if varible != "":
    print varible

varibles = [locu_search(locality), region_search(region), zip_search(zip)]
__I think the above should work but I get traceback 
error:
Traceback (most recent call last):
  File "C:/Users/dell/Documents/Python/MyProject/API_Projects/locu/locuAPI.py", 
line 70, in 
    varibles = [locu_search(locality), region_search(region), zip_search(zip)]
NameError: name 'locality' is not defined
The error points back to where this is:
varibles = [locu_search(locality), region_search(region), zip_search(zip)]
I don't understand why Python thinks that locality (and others) are not defined 
when I defined them at the very beginning. 


___After I get this new issue resolved, I will go back 
and review urllib query functions. 
 Thank you. 


 On Sunday, October 11, 2015 6:25 PM, Alan Gauld 
 wrote:
   
 

 On 11/10/15 22:22, Nym City wrote:
> import urllib2
> import json
>
> locu_api = 'redacted'
>
> ans=True
> while ans:
>    print ("""
>    1.Search by City
>    2.Search by State
>    3.Search by Zip
>    4.Exit/Quit
>    """)
>    ans=raw_input("What would you like to do? ")
>    if ans=="1":
>      print("\n Enter City")
>    elif ans=="2":
>      print("\n Search by State")
>    elif ans=="3":
>      print("\n Search by Zip")
>    elif ans=="4":
>      print("\n Goodbye")
>    elif ans !="":
>      print("\n Not Valid Choice Try again")
>
>

Note that you never set ans to any false value so it will keep on looping
until you do (by entering an empty string).

You probably need to use break to exit the loop whenb you get valid input.

> # def locu_search(query):
> #    api_key = locu_api
> #    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
> #    locality = query.replace(' ', '%20')
> #    final_url = url + '=' + locality + "=restaurant"

Note that you should not try to build the query string yourself, there are
functions in the standard library that will do most of the work and they
will be more reliable than anythong you can write yourself.

For example look at the urllib module (for 2.7, its the urlib.parse module
in v3) which contains several utility functions such as quote() and
urlencode()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

Re: [Tutor] Custom Function that Takes argument

2015-10-11 Thread Nym City via Tutor
Thank you for your feedback. I modified the code to reflect your suggestion and 
it worked. Now, I want to take it to the next level when I prompt the user to 
make a selection from a list of options and based on their input certain part 
of the code would execute. Here is the revised code:(site: https://locu.com/)

import urllib2
import json

locu_api = 'redacted'

ans=True
while ans:
    print ("""
    1.Search by City
    2.Search by State
    3.Search by Zip
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ")
    if ans=="1":
  print("\n Enter City")
    elif ans=="2":
  print("\n Search by State")
    elif ans=="3":
  print("\n Search by Zip")
    elif ans=="4":
  print("\n Goodbye")
    elif ans !="":
  print("\n Not Valid Choice Try again")


# city = input_city = raw_input('Please enter your city: ')
# region = input_region = raw_input('Please enter your state: ')
# zip = raw_input('What is your zip: ')

# def locu_search(query):
# api_key = locu_api
# url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
# locality = query.replace(' ', '%20')
# final_url = url + '=' + locality + "=restaurant"
# jason_obj = urllib2.urlopen(final_url)
# data = json.load(jason_obj)
# for item in data['objects']:
# print item['name'], item['phone'], item['street_address']
#
# def region_search(query):
# api_key = locu_api
# url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
# region = query.replace(' ', '%20')
# final_url = url + '=' + region + "=restaurant"
# jason_obj = urllib2.urlopen(final_url)
# data = json.load(jason_obj)
# for item in data['objects']:
# print item['name'], item['phone'], item['street_address'], 
item['locality']
#
# # def zip_search(query):
# # api_key = locu_api
# # url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
# # zip = query.replace(' ', '%20')
# # final_url = url + '=' + zip + "=restaurant"
# # jason_obj = urllib2.urlopen(final_url)
# # data = json.load(jason_obj)
# # for item in data['objects']:
# # print item['name'], item['phone'], item['street_address'], 
item['categories']
#
# # print zip_search(zip)
-
The part that is not working is the initial prompt for selection. The program 
prompts the user to make a selection, however it is not registering that 
selection rather repeats the prompt.

Hope my question make sense. Please let me know what I am missing. Thank you. 
Thank you. 


 On Monday, September 28, 2015 9:03 PM, Alan Gauld 
<alan.ga...@btinternet.com> wrote:
   
 

 On 29/09/15 00:45, Nym City via Tutor wrote:

> I am learning how to create custom functions and watched the
 > tutorial online that uses API for locu

Since most folks here probably don't know locu you should
maybe give us a URL. Then we can know what it is you are
talking about.

> I cannot figure out how to prompt a user to input their zip
 > code and use that information against the function.

The normal way is to pass it in as a parameter of the function:

def myfunc(aZipCode):
    print aZipCode

zip = raw_input('What is your zip: ')
myfunc(zip)


> import urllib2
> import json
>
> locu_api = 'not included here for obvious reasons'
> zip = raw_input('What is your zip: ')
>
> def zip_search(query):
>      api_key = locu_api
>      url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
>      zip = query.replace(' ', '%20')
>      final_url = url + '=' + zip + "=restaurant"
>      jason_obj = urllib2.urlopen(final_url)
>      data = json.load(jason_obj)
>      for item in data['objects']:
>          print item['name'], item['phone'], item['street_address'], 
>item['categories']
>
> print zip_search(zip_search())

Here you are passing the results of your own function
in as an argument to your function. That should give an
error since the inner call has no argument and the
function is looking for one.
You need to pass in your query string:

print zip_search(zip)

Always post any errors you get it helps us help you.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exception Handling

2015-10-03 Thread Nym City via Tutor
Thank you (Cameron and Alan) for your review and feedback. This solved the 
issue perfectly! 
 Thank you. 


 On Friday, October 2, 2015 11:49 PM, Cameron Simpson <c...@zip.com.au> 
wrote:
   
 

 On 03Oct2015 00:51, ALAN GAULD <alan.ga...@btinternet.com> wrote:
>On 02/10/15 23:57, Nym City via Tutor wrote:
>>socket.gaierror: [Errno 11004] getaddrinfo failed
>...
>>for name in ListOfHostNames:
>>    try:
>>        ResolveHostname = socket.gethostbyname(name)
>>        print(ResolveHostname)
>>        newFile.write(ResolveHostname + "\n")
>>        print(ResolveHostname)
>>    except socket.herror as e:
>>        newFile.write("No resolution available for %s" % (name) + "\n")
>
>You are catching herror but your code is resulting in gaierror.
>
>Add socket.gaierror to your except line.
>
>    except (socket.herror, socket.gaierror):
>        newFile.write("No resolution available for %s" % (name) + "\n")
>
>see if that works

Just a followon remark: always try to print the exception when you're not 
certain of what it will be or what to do with it. So I'd augument Alan's code 
like this:

    except (socket.herror, socket.gaierror) as e:
        newFile.write("No resolution available for %s: %s" % (name, e) + "\n")

Cheers,
Cameron Simpson <c...@zip.com.au>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Exception Handling

2015-10-02 Thread Nym City via Tutor
Hello,
I am trying to get IP addresses for about 500 public domains but I think for 
some where resolution is not available - my exception handling breaks the code. 
To troubleshoot, I added several Print statements to see what I was getting and 
it looks like my blank list is getting populated with domain names just fine 
and it starts to resolve addresses but less than half way though it errors out 
with the following message:
Traceback (most recent call last):
  File 
"C:/Users/Documents/Python/MyProject/HostnameResolution/HostnameResolution.py", 
line 15, in 
    ResolveHostname = socket.gethostbyname(name)
socket.gaierror: [Errno 11004] getaddrinfo failed
Here is the Code:

import socket

ListOfHostNames = []

with open('hostnames.csv', 'r') as f:
    for line in f:
    line = line.strip()
    ListOfHostNames.append(line)

newFile = open('hostnames.csv', 'w')
print(ListOfHostNames)

for name in ListOfHostNames:
    try:
    ResolveHostname = socket.gethostbyname(name)
    print(ResolveHostname)
    newFile.write(ResolveHostname + "\n")
    print(ResolveHostname)
    except socket.herror as e:
    newFile.write("No resolution available for %s" % (name) + "\n")
newFile.close()


Please advice. Thanks! Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beautiful Soup

2015-09-29 Thread Nym City via Tutor
Hello, I have personally found this tutorial to be helpful. Check it out: 
https://www.youtube.com/watch?v=3xQTJi2tqgk Thank you. 


 On Tuesday, September 29, 2015 12:05 PM, Joel Goldstick 
 wrote:
   
 

 On Tue, Sep 29, 2015 at 11:47 AM, Crusier  wrote:

> Hi
>
> I have recently finished reading "Starting out with Python" and I
> really want to do some web scraping. Please kindly advise where I can
> get more information about BeautifulSoup. It seems that Documentation
> is too hard for me.
>
> Furthermore, I have tried to scrap this site but it seems that there
> is an error (). Please
> advise what I should do in order to overcome this.
>
>
> from bs4 import BeautifulSoup
> import urllib.request
>
> HKFile = urllib.request.urlopen("
> https://bochk.etnet.com.hk/content/bochkweb/tc/quote_transaction_daily_history.php?code=2388
> ")
> HKHtml = HKFile.read()
> HKFile.close()
>
> print(HKFile)
>
> Thank you
> Hank
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>

many people find this package to be easier to use than the built in python
support for reading urls:

http://docs.python-requests.org/en/latest/

-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Custom Function that Takes argument

2015-09-28 Thread Nym City via Tutor
Hello,
I am learning how to create custom functions and watched the tutorial online 
that uses API for locu to do cool stuff. I wanted to go little bit beyond the 
tutorial and add my own features. The problem that I am running into is that I 
cannot figure out how to prompt a user to input their zip code and use that 
information against the function. here is my code:
import urllib2
import json

locu_api = 'not included here for obvious reasons'
zip = raw_input('What is your zip: ')

def zip_search(query):
    api_key = locu_api
    url = 'https://api.locu.com/v1_0/venue/search/?api_key=' + api_key
    zip = query.replace(' ', '%20')
    final_url = url + '=' + zip + "=restaurant"
    jason_obj = urllib2.urlopen(final_url)
    data = json.load(jason_obj)
    for item in data['objects']:
    print item['name'], item['phone'], item['street_address'], 
item['categories']

print zip_search(zip_search())

 Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error

2015-09-18 Thread Nym City via Tutor
-Perfect. Thank you. 


 On Tuesday, September 15, 2015 5:00 AM, Alan Gauld 
<alan.ga...@btinternet.com> wrote:
   
 

 On 15/09/15 02:41, Nym City via Tutor wrote:
> Hello,
> I am also just trying to understand this code and could not understand the 
> print statement.I would have just used:
>  print('cost:$',gross_cost)
> Do you mind telling little bit about what:
> print('cost: ${:.2f}'.format(gross_cost))
> does do and why one would use this over my plain version?

When in doubt use the >>> prompt:

 >>> gross_cost = 4.6
 >>> print('cost: $',gross_cost)
cost: $4.6
 >>> print('cost: ${:.2f}'.format(gross_cost))
cost: $4.60

So the .2f forces the output to be formatted as a floating
point number with 2 digits after the decimal point.
And the format() inserts the values into the {} markers
in the string to which its attached. Another longer example:

 >>> quantity = 4
 >>> unit_cost = 4
 >>> tax = 0.1
 >>> print('''I bought {} items at ${:.2f} with {}% tax,
... making a total cost of: ${:.2f}
... '''.format(quantity,
...          unit_cost,
...          int(tax*100),
...          quantity * unit_cost * (1+tax)))
I bought 4 items at $4.00 with 10% tax
making a total cost of: $17.60

Notice this time that :.2f forced the integer unit_cost(4)
to be shown as a float with 2 decimal places(4.00).

There are lots of other codes you can use to modify the
formatting.

Check the language reference in the docs under 'formatting':

https://docs.python.org/3/library/string.html#formatstrings

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Syntax error and EOL Error

2015-09-07 Thread Nym City via Tutor
Hello,
Thank you for your response. I have made updates and here is the new code:
import csv
DomainList = []

domains = open('domainlist.csv', 'rb')
DomainList = csv.reader(domains)
DomainList = [column[1] for column in DomainList(str.rstrip('/'))]
print(DomainList)
For "DomainList = [column[1] for column in DomainList(str.rstrip('/'))]" line, 
I am getting following error:TypeError: '_csv.reader' object is not callable
Doing some research, i thought it might be because I am importing the csv as 
'r' only so i tried changing it to 'rb' and 'w' but received the same error.
Also, not sure if the next error is because of the above issue but I get syntax 
error when I do the last print and I do not see any syntax issue with it.
As always, thanks in advance. 
 Thank you. 


 On Friday, September 4, 2015 3:23 AM, Peter Otten <__pete...@web.de> wrote:
   
 

 Nym City via Tutor wrote:

>  import csv
> DomainList = []
> 
> domains = open('domainlist.csv', 'r')
> DomainList = csv.reader(domains)
> DomainList = [column[1] for column in DomainList]
> DomainList = (str(DomainList).rstrip('/')
> print('\n'.join(DomainList))
> 
> 
> I keep getting EOL error on the second the last line and syntax error on
> the last print line. Even when I change the print line to say the
> following: print(DomainList) Please advise. Thank you in advance.
> Thank you.

Look at the lines preceding the one triggering the SyntaxError. Usually 
there is an opening (, [ or { which doesn't have a matching closing 
counterpart. 

In your case count the parens in the line

> DomainList = (str(DomainList).rstrip('/')

By the way, even without error this line will not have the desired effect.
Given 

>>> DomainList = ["facebook.com/", "twitter.com/"]

converting to string gives

>>> str(DomainList)
"['facebook.com/', 'twitter.com/']"

and as that doesn't end with a "/" applying the rstrip() method has no 
effect. Instead you can modify the line

> DomainList = [column[1] for column in DomainList]

where you can invoke the rstrip() method on every string in the list.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Syntax error and EOL Error

2015-09-04 Thread Nym City via Tutor
Hello,
I am working with a csv file that has following sample data:
Rank    URL    Linking Root Domains
1    facebook.com/    9616487
2    twitter.com/    6454936
3    google.com/    5868081
4    youtube.com/    5442206
5    wordpress.org/    4051288

In my program, I am reading in this csv file, taking only data from the second 
column and striping off the leading "/"
 import csv
DomainList = []

domains = open('domainlist.csv', 'r')
DomainList = csv.reader(domains)
DomainList = [column[1] for column in DomainList]
DomainList = (str(DomainList).rstrip('/')
print('\n'.join(DomainList))


I keep getting EOL error on the second the last line and syntax error on the 
last print line. Even when I change the print line to say the following:
print(DomainList)
Please advise. Thank you in advance. 
 Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing back to same CSV in the next column

2015-08-23 Thread Nym City via Tutor
Hello,
Here is my final script. It is doing what I wanted it to. I wanted to just 
share it as a final product and thank you all for your feedback on the various 
previous revisions. 

import socket

ListOfIPAddresses = []

with open('top500ips.csv', 'r') as f:
    for line in f:
    line = line.strip()
    ListOfIPAddresses.append(line)

newFile = open('top500ips.csv', 'w')

for address in ListOfIPAddresses:
    try:
    ResolvedAddresses = socket.gethostbyaddr(address)[0]
    newFile.write(ResolvedAddresses + \n)
    # print(ResolvedAddresses)
    except socket.herror as e:
    newFile.write(No resolution available for %s % (address) + \n)



If you have any suggestions, please do share. 

Thank you. 


 On Monday, August 17, 2015 4:35 AM, Alan Gauld alan.ga...@btinternet.com 
wrote:
   
 

 On 17/08/15 02:51, Nym City via Tutor wrote:
  the output of the gethostbyaddr module includes three item
 (hostname, aliaslist, ipaddrlist). However, in my output
 I just what the hostname field. So I created a list but
 I am not able to pull out just the [0] item from this
  and instead I get the following error:
  TypeError: 'int' object is not subscriptable.

 for line in in_file:
      try:
          name = socket.gethostbyaddr(line.strip())
          ListOfIPAddresses.append(name)
          out_file.write(str(ListOfIPAddresses))[0]

Look at that last line and break it down.
Where is the index operation?

It's right at the end so it applies to the output
of the write() operation. You need it against
the list, before you convert to string and before
you write to file.

 Also, could you please give some explanation of '\t'.

Its the tab character. it inserts a tab, just like hitting
the tab key on the keyboard.
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] SyntaxError: unexpected EOF while parsing

2015-08-18 Thread Nym City via Tutor
Hello,
I would like to know what is the reason and proper solution for the following 
error: SyntaxError: unexpected EOF while parsing.
I am get this error usually when I am troubleshooting and comment out portion 
of the code and add Print() statements to see where the issues are in my code. 
However, I am not able to do that with the above error - which points me after 
the last line of the code where nothing exists. 
Please advise. Thank you in advance. 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing back to same CSV in the next column

2015-08-17 Thread Nym City via Tutor
Hi,
Thank you for your response. I fixed the parenthesis and it worked.

I made little modification below. By default the output of the gethostbyaddr 
module includes three item (hostname, aliaslist, ipaddrlist). However, in my 
output I just what the hostname field. So I created a list but I am not able to 
pull out just the [0] item from this and instead I get the following 
error:TypeError: 'int' object is not subscriptable. I looked up the error but 
most examples that I found were not applicable to my code purpose. 

import socket

in_file = open('top500ips.csv', 'r')
out_file = open('top500ips_out.csv', 'w')

ListOfIPAddresses = []

for line in in_file:
    try:
    name = socket.gethostbyaddr(line.strip())
    ListOfIPAddresses.append(name)
    out_file.write(str(ListOfIPAddresses))[0]
    except socket.herror:
    out_file.write(line + '\t' + No resolution available for )

in_file.close()
out_file.close()
-
Also, could you please give some explanation of '\t'. 


Thanks.


  Thank you. 


 On Sunday, August 16, 2015 5:58 PM, Alan Gauld alan.ga...@btinternet.com 
wrote:
   
 

 On 16/08/15 22:42, Nym City wrote:
 import socket
 import csv

You don't need csv, you aren't using it.

 in_file = open('top500ips.csv', 'r')
 out_file = open('top500ips_out.csv', 'w')

 for line in in_file:
    try:
        name = socket.gethostbyaddr(line.strip())
        out_file.write(line + '\t' + (str(name))

count the parens in the line above...

    except socket.herror:
        out_file.write(line + '\t' + errrMsg)

 in_file.close()
 out_file.close()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing back to same CSV in the next column

2015-08-16 Thread Nym City via Tutor
Hello,
Thank you for your guidance. Using your pseudocode I have put together the 
following:


import socket
import csv

in_file = open('top500ips.csv', 'r')
out_file = open('top500ips_out.csv', 'w')

for line in in_file:
    try:
    name = socket.gethostbyaddr(line.strip())
    out_file.write(line + '\t' + (str(name))
    except socket.herror:
    out_file.write(line + '\t' + errrMsg)

in_file.close()
out_file.close()
-
I am getting few errors. Such as, I had to add 'str in front of (name) to 
address TypeError: Can't convert 'int' object to str implicitly.
Also, not sure why I keep getting SyntaxError: invalid syntax pointing to the 
except line.
Please advise.
Thank you.




 Thank you. 


 On Wednesday, August 12, 2015 7:07 AM, Nym City via Tutor 
tutor@python.org wrote:
   
 

 Hello,
Please find the two requested files attached. The 'before' file is what I am 
reading into my program. The 'after' file is what I would like to have my 
output to look like. Ideally, I want it to be the same file but if its easier 
to create a new file for the output - that is ok too.
 I do not have the understanding of binary vs text modes. But I have found this 
online, can't say I understand it though: 
http://fileinfo.com/help/binary_vs_text_files  Thank you. 



    On Tuesday, August 11, 2015 4:10 AM, Alan Gauld alan.ga...@btinternet.com 
wrote:
  
 

 On 11/08/15 01:23, Nym City via Tutor wrote:

 import socket
 import csv

 ListOfIPAddresses = []

 with open('top500ips.csv', 'rb') as f:
      for line in f:
          line = line.strip()
          ListOfIPAddresses.append(line)
 f.close()

You don;t need the f.close(). The 'with' structiure
does that automatically.

 # print(ListOfIPAddresses)
 newFile = open('top500ips.csv', 'w')

The original file was opened in binary mode, you
are opening it here in text mode. Are you sure
that's correct? Do you undertand the significance
of binary v text modes?

Also 'w' mode effectively creates a new empty file
so you will need to recreate every line that was
in the input file. Its usually better to rename
the original file to something like top500ips.bak
and then create a new file with the original name.
If all goes well you can delete the .bak version,
if something goes wrong you can rename it back
to the original.

 for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)[0]

You save the result into the variable but do nothing with it.
The next time round the loop the result will be overwritten and the 
previous one lost. You are not writing anything to the file.

      except socket.herror as e:
          print(No resolution available for %s: %s % (address, e))
          newFile.write.(ResolvedAddresses + \n)Thank you.

You are only writing to the file when you get the error.
But at that point ResolvedAddresses will contain the result from
the previous iteration of the loop so it may well be misleading.
You in effect only write the host to file for the entry
before lines that cause errors. I'm pretty sure thats not what
you want.

The other thing is that you are only writing the name. So your
file will only contain a short column of names. Again I don't
think that's what you wanted.

Can you send us an example of before and after?
ie about 5 lines of content from the file before you start
and what it should look like after you finish?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing back to same CSV in the next column

2015-08-12 Thread Nym City via Tutor
Hello,
Please find the two requested files attached. The 'before' file is what I am 
reading into my program. The 'after' file is what I would like to have my 
output to look like. Ideally, I want it to be the same file but if its easier 
to create a new file for the output - that is ok too.
 I do not have the understanding of binary vs text modes. But I have found this 
online, can't say I understand it though: 
http://fileinfo.com/help/binary_vs_text_files  Thank you. 



 On Tuesday, August 11, 2015 4:10 AM, Alan Gauld 
alan.ga...@btinternet.com wrote:
   
 

 On 11/08/15 01:23, Nym City via Tutor wrote:

 import socket
 import csv

 ListOfIPAddresses = []

 with open('top500ips.csv', 'rb') as f:
      for line in f:
          line = line.strip()
          ListOfIPAddresses.append(line)
 f.close()

You don;t need the f.close(). The 'with' structiure
does that automatically.

 # print(ListOfIPAddresses)
 newFile = open('top500ips.csv', 'w')

The original file was opened in binary mode, you
are opening it here in text mode. Are you sure
that's correct? Do you undertand the significance
of binary v text modes?

Also 'w' mode effectively creates a new empty file
so you will need to recreate every line that was
in the input file. Its usually better to rename
the original file to something like top500ips.bak
and then create a new file with the original name.
If all goes well you can delete the .bak version,
if something goes wrong you can rename it back
to the original.

 for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)[0]

You save the result into the variable but do nothing with it.
The next time round the loop the result will be overwritten and the 
previous one lost. You are not writing anything to the file.

      except socket.herror as e:
          print(No resolution available for %s: %s % (address, e))
          newFile.write.(ResolvedAddresses + \n)Thank you.

You are only writing to the file when you get the error.
But at that point ResolvedAddresses will contain the result from
the previous iteration of the loop so it may well be misleading.
You in effect only write the host to file for the entry
before lines that cause errors. I'm pretty sure thats not what
you want.

The other thing is that you are only writing the name. So your
file will only contain a short column of names. Again I don't
think that's what you wanted.

Can you send us an example of before and after?
ie about 5 lines of content from the file before you start
and what it should look like after you finish?


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Writing back to same CSV in the next column

2015-08-11 Thread Nym City via Tutor
Hello,
Wanted to write back to see if anyone  had the chance to review my previous 
request. 

import socket
import csv

ListOfIPAddresses = []

with open('top500ips.csv', 'rb') as f:
    for line in f:
    line = line.strip()
    ListOfIPAddresses.append(line)
f.close()

# print(ListOfIPAddresses)
newFile = open('top500ips.csv', 'w')

for address in ListOfIPAddresses:
    try:
    ResolvedAddresses = socket.gethostbyaddr(address)[0]
    except socket.herror as e:
    print(No resolution available for %s: %s % (address, e))
    newFile.write.(ResolvedAddresses + \n)Thank you. 


 On Sunday, August 2, 2015 7:47 PM, Nym City via Tutor tutor@python.org 
wrote:
   
 

 Hello,
Below is my program where I am reading a list of IPs from a CSV file and 
running it through the socket module. The result of the computation is stored 
in a variable named ResolvedAddresses. However, there are some that are not 
resolved and for those there is an exception.
The task that I am trying to complete now is taking the output of the 
ResolvedAddresses and the content of the exception and write is back out to the 
same CSV file where the data is originally imported from. Ideally, I would like 
this new information to be in the column B and match with column A.
Thank you in advance. And please let me know if what I am trying to accomplish 
is not clear and I'll try to explain it better.

Here is the code: https://bpaste.net/show/01a412f72fa1

 Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Writing back to same CSV in the next column

2015-08-02 Thread Nym City via Tutor
Hello,
Below is my program where I am reading a list of IPs from a CSV file and 
running it through the socket module. The result of the computation is stored 
in a variable named ResolvedAddresses. However, there are some that are not 
resolved and for those there is an exception.
The task that I am trying to complete now is taking the output of the 
ResolvedAddresses and the content of the exception and write is back out to the 
same CSV file where the data is originally imported from. Ideally, I would like 
this new information to be in the column B and match with column A.
Thank you in advance. And please let me know if what I am trying to accomplish 
is not clear and I'll try to explain it better.

Here is the code: https://bpaste.net/show/01a412f72fa1

 Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Module

2015-07-30 Thread Nym City via Tutor
Writing to share an update on my previous request.
So, after reviewing my code over, it seems like my last print statement 
print(ResolvedAddresses) was not properly indented inside the for loop - and 
for that reason the output was coming our as empty.
After I aligned that with the rest of the loop, it worked fine.
I still have couple parts to add to this project but at least this hurdle is 
behind.
Thank you. 


 On Tuesday, July 28, 2015 4:49 AM, Nym City via Tutor tutor@python.org 
wrote:
   
 

 Hi Martin,
Thank you for taking the time to write such a detailed response. Very much 
appreciate it. 

I tried the code again with modifications that you suggested and even though 
none of the public addresses resolved; I did get little more details.
I am still working on finding a solution for this and I will share it here when 
I have it.

 Thank you. 


    On Sunday, July 26, 2015 6:59 PM, Martin A. Brown mar...@linux-ip.net 
wrote:
  
 

 
Hello Nym,

 Here is the updated code: https://bpaste.net/show/358583e1a0bd

It's short.  I have included inline here:

  import socket

  ListOfIPAddresses = []

  with open('top500ips.csv', 'r') as f:
      for line in f:
          line = line.strip()
          ListOfIPAddresses.append(line)

  for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)
      except:
          print('No Resolution Available')

  print(ResolvedAddresses)

 The issue that I am running into now is that for some reason, the 
 script is not resolving known-public IP addresses that I am 
 passing through. For example, some of the IPs, that I have used 
 are for sites like facebook (173.252.120.6) github 
 (207.97.227.239), however the script is not able to resolve them.

 But its interesting that I am able to resolve them using nslookup 
 on windows command prompt. Which means my laptop's DNS setting is 
 fine.

The apparent (?) DNS lookup failure
---
At time X, you run your Python program and something (perhaps in the 
DNS resolution process) fails and you see No Resolution Available, 
but you do not know what has failed, nor for which address lookup.

At time Y, you run 'nslookup' at the shell prompt, receive an answer 
and conclude that your script is operating properly.  While this is 
may appear logical, it is an incorrect conclusion.

One coding error (a dangerous habit to perpetuate)
--
When performing the DNS lookup, you are using something called 
a 'bare except'.  This will catch any and all errors, even if it's 
something unrelated like, for example, a signal.  This is a bad and 
dangerous habit.  In general, you should catch only the exceptions 
that you can do something about.

In addition, this will offer you more information about the problem. 
Here's a simple example, where I'm only changing two lines:

  for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)
      except socket.herror as e:
          print(No resolution available for %s: %s % (address, e))

This will give you a little more information about what, 
specifically, the failure is in your call to socket.gethostbyaddr()

Comment on NXdomain responses
-
I picked at random an address that had no PTR record and tried to 
call socket.gethostbyaddr('8.97.227.2').  What do you think I got 
for my trouble?  When running through the code block above, I saw 
the following output to my terminal:

  No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error)

In short, there is no guarantee that anybody has properly set up 
reverse DNS entries (DNS PTR records) for the addresses you are 
looking up.  Although the vast majority of lookups occur 
successfully and smoothly, there are many things that can go wrong 
in the network and on an end host which can cause transient errors 
during DNS lookups, and it is possible that you have already 
encountered some of these problems (even though I would not expect 
to hit very many errors looking up PTR records for only 500 IPs).

May I wish you good luck resolving not just your addresses, but also 
your problem!

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Module

2015-07-28 Thread Nym City via Tutor
Hi Martin,
Thank you for taking the time to write such a detailed response. Very much 
appreciate it. 

I tried the code again with modifications that you suggested and even though 
none of the public addresses resolved; I did get little more details.
I am still working on finding a solution for this and I will share it here when 
I have it.

 Thank you. 


 On Sunday, July 26, 2015 6:59 PM, Martin A. Brown mar...@linux-ip.net 
wrote:
   
 

 
Hello Nym,

 Here is the updated code: https://bpaste.net/show/358583e1a0bd

It's short.  I have included inline here:

  import socket

  ListOfIPAddresses = []

  with open('top500ips.csv', 'r') as f:
      for line in f:
          line = line.strip()
          ListOfIPAddresses.append(line)

  for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)
      except:
          print('No Resolution Available')

  print(ResolvedAddresses)

 The issue that I am running into now is that for some reason, the 
 script is not resolving known-public IP addresses that I am 
 passing through. For example, some of the IPs, that I have used 
 are for sites like facebook (173.252.120.6) github 
 (207.97.227.239), however the script is not able to resolve them.

 But its interesting that I am able to resolve them using nslookup 
 on windows command prompt. Which means my laptop's DNS setting is 
 fine.

The apparent (?) DNS lookup failure
---
At time X, you run your Python program and something (perhaps in the 
DNS resolution process) fails and you see No Resolution Available, 
but you do not know what has failed, nor for which address lookup.

At time Y, you run 'nslookup' at the shell prompt, receive an answer 
and conclude that your script is operating properly.  While this is 
may appear logical, it is an incorrect conclusion.

One coding error (a dangerous habit to perpetuate)
--
When performing the DNS lookup, you are using something called 
a 'bare except'.  This will catch any and all errors, even if it's 
something unrelated like, for example, a signal.  This is a bad and 
dangerous habit.  In general, you should catch only the exceptions 
that you can do something about.

In addition, this will offer you more information about the problem. 
Here's a simple example, where I'm only changing two lines:

  for address in ListOfIPAddresses:
      try:
          ResolvedAddresses = socket.gethostbyaddr(address)
      except socket.herror as e:
          print(No resolution available for %s: %s % (address, e))

This will give you a little more information about what, 
specifically, the failure is in your call to socket.gethostbyaddr()

Comment on NXdomain responses
-
I picked at random an address that had no PTR record and tried to 
call socket.gethostbyaddr('8.97.227.2').  What do you think I got 
for my trouble?  When running through the code block above, I saw 
the following output to my terminal:

  No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error)

In short, there is no guarantee that anybody has properly set up 
reverse DNS entries (DNS PTR records) for the addresses you are 
looking up.  Although the vast majority of lookups occur 
successfully and smoothly, there are many things that can go wrong 
in the network and on an end host which can cause transient errors 
during DNS lookups, and it is possible that you have already 
encountered some of these problems (even though I would not expect 
to hit very many errors looking up PTR records for only 500 IPs).

May I wish you good luck resolving not just your addresses, but also 
your problem!

-Martin

-- 
Martin A. Brown
http://linux-ip.net/


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Module

2015-07-25 Thread Nym City via Tutor
Thank you all for your responses. I have taken your feedback and made changes 
to my code.
-Danny, per your suggestion, I have renamed some of my variables to make their 
purpose little more clearer.
- Alan,  I have created a new host list (ResolvedAddresses) which is storing 
the output from socket.gethostbyaddr(address).

Here is the updated code: https://bpaste.net/show/358583e1a0bd
The issue that I am running into now is that for some reason, the script is not 
resolving known-public IP addresses that I am passing through. For example, 
some of the IPs, that I have used are for sites like facebook (173.252.120.6) 
github (207.97.227.239), however the script is not able to resolve them.
But its interesting that I am able to resolve them using nslookup on windows 
command prompt. 
Which means my laptop's DNS setting is fine.
Looking forward to your feedback. Thanks!

 Thank you. 


 On Monday, July 20, 2015 5:00 AM, Alan Gauld alan.ga...@btinternet.com 
wrote:
   
 

 On 20/07/15 00:55, Nym City via Tutor wrote:
 Thank you for your response. I gave it another try:
 As suggested, first I ran the concept just in the terminal, and it worked 
 fine:
 names =['173.252.120.6', '98.139.183.24']
 import socket
 for name in names:
      socket.gethostbyaddr(name)
      print(name)

 output:
 ('edge-star-shv-12-frc3.facebook.com', [], ['173.252.120.6'])
 ('ir2.fp.vip.bf1.yahoo.com', [], ['98.139.183.24'])

Remember that the  prompt evaluates your results and
automatically prints them for you.

Thus

  5 + 3
8

But if you put code in a script and execute it the interpreter
does NOT print out arbitrary expressions so a file add.py
containing just

5 + 3

will not output anything, you need the print function:

print(5+3)

to see the result.

So in the interpreter your gethostbyaddr() function is
evaluated AND printed. But in a script it will only be
evaluated...

 import csv
 import socket

 domains = []

 with open('top500ips.csv', 'r') as f:
      for line in f:
          line = line.strip()
          domains.append(line)

You are importing csv but not using it to read your file.
But I'll ignore that for now! (in fact it looks like the
csv file really only contains the IP addresses, one per
line so csv is probably redundant here.)

Also you could do all of the above in a single line with:

domains = [line.strip() for line in open('top500ips.csv')]

But none of that matters for your specific issue...

 for name in domains:
      socket.gethostbyaddr(name)
      print(name)

Notice here that you run the gethostbyaddr() function but
do nothing with the result. You don't store it and you
don't print it. Instead you print the initial name that
you passed in, which does not change.

You need to create a variable to store the host result
and then print that host. And since you want to store a
set of hosts you probably want to append the results to
a list of some kind (or a dictionary based on your names?)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Module

2015-07-19 Thread Nym City via Tutor
Thank you for your response. I gave it another try:
As suggested, first I ran the concept just in the terminal, and it worked fine:
 names =['173.252.120.6', '98.139.183.24']
 import socket
 for name in names:
    socket.gethostbyaddr(name)
    print(name)
  
output:  
('edge-star-shv-12-frc3.facebook.com', [], ['173.252.120.6'])
('ir2.fp.vip.bf1.yahoo.com', [], ['98.139.183.24'])
However, when I run it in a program, from a CSV file, it just outputs the 
content of the CSV file without running it thought socket.gethostbyaddr():
import csv
import socket

domains = []

with open('top500ips.csv', 'r') as f:
    for line in f:
    line = line.strip()
    domains.append(line)

for name in domains:
    socket.gethostbyaddr(name)
    print(name)
output: 
173.252.120.6
98.139.183.24

What am I missing? Thank in advance.
 Thank you. 


 On Saturday, July 18, 2015 7:09 PM, Danny Yoo danny@gmail.com wrote:
   
 

 
On Jul 18, 2015 3:50 PM, Nym City via Tutor tutor@python.org wrote:

 Thank you all for your responses. I have a follow up question:

 So if gethostbyname_ex() takes only a single hostname string, how can I use 
 it to go through a list of hostnames and get their IP resolution as an output?

Look into loops.  If you have a function that works on a single thing, you can 
use a loop to apply that function for each element in a list.  Any good 
tutorial should show how to do this.Let us know if you run into difficulties.


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Module

2015-07-18 Thread Nym City via Tutor
Thank you all for your responses. I have a follow up question:

So if gethostbyname_ex() takes only a single hostname string, how can I use it 
to go through a list of hostnames and get their IP resolution as an output? 

This would mean,socket.gethostbyaddr() would also not work for my second 
project.
 Thank you. 


 On Monday, July 13, 2015 2:59 AM, Alan Gauld alan.ga...@btinternet.com 
wrote:
   
 

 On 12/07/15 23:36, Nym City via Tutor wrote:

 import csv
 import socket

 domains = open('top500domains.csv', 'r')
 for domain in domains:
      domain = socket.gethostbyname(str(domains))

You are passing your file object to gethostbyname()

      print(domains + \n)
 For the code above, I receive the following error on run: socket.gaierror: 
 [Errno -2] Name or service not known.

 Variation 2:
 import csv
 import socketdomains = []

I assume the domains bit is on a separate line?


 with open('top500domains.csv', 'r') as f:
      for line in f:
          line = line.strip()
          domains.append(line)

 hostbyname_ex = socket.gethostbyname_ex(str(domains))

Again you are passing the entire list to gethostbyname_ex()


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Module

2015-07-12 Thread Nym City via Tutor
Hello,
I am working on a 2 individual programs. In the first program, I am taking in a 
list of domains in .csv format and want to get the associated IPs. 
In the second program, I want to import in a list of IP .csv and than get the 
associated domains out.
Eventually I want to get both of the above programs to write out to the same 
.csv file that they are reading from- in the next column. But I am not there 
yet.
Doing research I came across the following 
example:http://python.about.com/od/pythonstandardlibrary/qt/dnscheck.htm
From the link above, doing domain - IP would be easier to begin with. Here are 
the two variations of my code:
Variation 1:
import csv
import socket

domains = open('top500domains.csv', 'r')
for domain in domains:
    domain = socket.gethostbyname(str(domains))
    print(domains + \n)
For the code above, I receive the following error on run: socket.gaierror: 
[Errno -2] Name or service not known.
Variation 2:
import csv
import socketdomains = []

with open('top500domains.csv', 'r') as f:
    for line in f:
    line = line.strip()
    domains.append(line)

hostbyname_ex = socket.gethostbyname_ex(str(domains))
print(hostbyname_ex)
I receive the same error as the first variation. 
Please share your advice and let me know what I am doing wrong. The 
top500domains.csv list is attached. 

Thanks in advance!




 Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Loop not iterating

2015-07-05 Thread Nym City via Tutor
This is pretty cool, Thank you for explaining! I liked the second solution. 
Thank you. 


 On Sunday, July 5, 2015 2:10 AM, Steven D'Aprano st...@pearwood.info 
wrote:
   
 

 Hi Nym, sorry your code's formatting is broken again. I've tried my best 
to fix it below:

On Fri, Jul 03, 2015 at 09:04:10PM +, Nym City via Tutor wrote:
 Thank to very much for replying.  The second solution that you proposed 
 worked perfectly:
 
 import csv
 domains = open('top500domains.csv')
 domainsReader = csv.reader(domains)
 domains = [https://www.; + row[1] for row in domainsReader]
 for domain in domains:
    print(domain) 
 
 The above solution is perfect and simple. It allows me to easily 
 insert text such as https://www.  in the beginning of my strings or 
 at the end.

 However, something else that came to mind was how would you break the 
 string and insert new text in the middle. For 
 example:www.lovepython.com I want to insert lesson1. after the 
 second period above. So it would come back as: 
 www.lovepython.lesson1.com

The most general way to do this is with string slicing. You have to 
build a new string:

s = www.lovepython.com
# find the 1st period
i = s.find('.')
# and the second:
i = s.find('.', i+1)
# slice just before the second dot
new_s = s[:i] + .lesson1 + s[i:]
print(new_s)



 First I thought row[1] in the code above 
 referred to the first place in the beginning of the string. So I tried 
 to change that number around but it did not work. I have a feeling I 
 might be mixing few concepts together... Thank you.


row[1] refers to item 1 in the list row, it has nothing to do with dots 
in the string.

The best tool for learning Python is to play with the interactive 
interpeter. Start up Python to get a command prompt. By default, the 
prompt is   but I prefer to use py . Now just type commands as 
needed. Python will automatically display them, or you can use print.


py row = [This, that, www.lovepython.com.au/, blah blah]
py row[0]
'This'
py row[2]
'www.lovepython.com.au/'
py s = row[2]
py s.find(.)  # first dot
3
py s.find(., 4)  # second dot
14
py s[:14]  # slice up to the second dot
'www.lovepython'
py s[14:]  # and from the second dot onwards
'.com.au/'


Here's another way to do it:

py s = row[2]
py L = s.split(.)
py print(L)
['www', 'lovepython', 'com', 'au/']
py L.insert(2, lesson999)
py print(L)
['www', 'lovepython', 'lesson999', 'com', 'au/']
py s = '.'.join(L)
py print(s)
www.lovepython.lesson999.com.au/


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Loop not iterating

2015-07-03 Thread Nym City via Tutor
Thank to very much for replying.  The second solution that you proposed worked 
perfectly:

import csvdomains = open('top500domains.csv')domainsReader = 
csv.reader(domains)domains = [https://www.; + row[1] for row in 
domainsReader]for domain in domains:    print(domain) The above solution is 
perfect and simple. It allows me to easily insert text such as https://www.  
in the beginning of my strings or at the end. However, something else that came 
to mind was how would you break the string and insert new text in the middle. 
For example:www.lovepython.com
I want to insert lesson1. after the second period above. So it would come 
back as: www.lovepython.lesson1.comFirst I thought row[1] in the code above 
referred to the first place in the beginning of the string. So I tried to 
change that number around but it did not work. I have a feeling I might be 
mixing few concepts together... Thank you. 


 On Monday, June 29, 2015 10:19 PM, Steven D'Aprano st...@pearwood.info 
wrote:
   
 

 On Tue, Jun 30, 2015 at 01:05:13AM +, Nym City wrote:
 Hello all,
 Thank you for your time and tips. The reason why I decided to create a 
 loop is because the output is cleaner and did not require any 
 formatting. However, based on what I have learned from all of your 
 responses, that is not going to work with what I am trying to do.

There's no reason why a loop wouldn't work, although there may be better 
solutions, and a loop is a much better solution to what you have below.


 Here is the updated code:
 import csvdomains = open('top500domains.csv')domainsReader = 
 csv.reader(domains)domainLists = 
 list(domainsReader)print('\n'.join([https://www . + str(lst) for lst 
 in domainLists]), sep='\n')

Please ensure you post as *plain text*, not HTML (so called rich text 
or formatted text). When you post as HTML, many mail programs mangle 
the code and destroy the formatting, as you can see above.

Reconstructing what the code should look like on five separate lines:

import csv
domains = open('top500domains.csv')
domainsReader = csv.reader(domains)
domainLists = list(domainsReader)
print('\n'.join([https://www . + str(lst) for lst in domainLists]), sep='\n')


The first three lines seem to be correct, but the next:

    domainLists = list(domainsReader)

is unnecessary and should be deleted. However, the last line does too 
much, and the wrong thing too. A simple loop is *much* better here, 
since you just print the domains and don't need to keep them for further 
processing:

for row in domainLists:
    # row is a list of *one or more* fields; in this case, there
    # is only one field per row, the domain name
    domain = https://www.; + row[0]
    print(domain)

Suppose that you do need to keep the domains for later, as well as print 
them. Then you can do this instead:


import csv
domains = open('top500domains.csv')
domainsReader = csv.reader(domains)
# Make a list of the domains.
domains = [https://www.; + row[0] for row in domainsReader]
# Print each domain.
for domain in domains:
    print(domain)
# Now continue to do more work on the list of domains...



-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Loop not iterating

2015-06-29 Thread Nym City via Tutor
Hello all,
Thank you for your time and tips. The reason why I decided to create a loop is 
because the output is cleaner and did not require any formatting. However, 
based on what I have learned from all of your responses, that is not going to 
work with what I am trying to do.
Here is the updated code:
import csvdomains = open('top500domains.csv')domainsReader = 
csv.reader(domains)domainLists = 
list(domainsReader)print('\n'.join([https://www . + str(lst) for lst in 
domainLists]), sep='\n')

-
It is almost how I want it - however, I cannot seem to figure out how to format 
the output to drop the [' '] from the output.
For example, here is how the output comes up:https://www .['facebook.com/']

I would like to get it in:https://www.facebook.com
Please suggest what is the best, as well as the easiest way to format data out 
of lists. (I image myself creating a lot of lists and would be helpful to know 
how to generally handle formatting with lists) 
Thanks in advance! :))




 Thank you. 


 On Sunday, June 28, 2015 10:57 PM, Steven D'Aprano st...@pearwood.info 
wrote:
   
 

 Hi Nym, and welcome,

On Sun, Jun 28, 2015 at 07:32:40PM +, Nym City via Tutor wrote:

[...]
 for domain in domainLists:
      something = (www. + str(domain))
 print(something)
 
 My program reads in a CSV file that has 500 list of domains. However, 
 when I save the output of my loop to the variable name something - 
 and later print something it contains only 1 entry from my csv file.

Of course it does. Each time you go through the loop, you change the 
value of something to the new value.

If you do this:

x = 23
x = 42
print(x)


What do you expect to print? Hopefully 42.

If you want multiple values, you need something like a list:

x = []
x.append(23)
x.append(42)
print(x)


Of course, in *this* case, there is an easier way to create a list with 
two values:

x = [23, 42]

but in the general case where you don't know how many values you will 
have, you need to add them using append inside a loop.


-- 
Steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Loop not iterating

2015-06-28 Thread Nym City via Tutor
Hello,
I am working on my second program and its a two part progam that I want to 
design However, i am not sure what silly mistake I am making on the first part:
Here is my code:

| 2
3
4
5
6
7 | import csv
domains = open('top500domains.csv')
domainsReader = csv.reader(domains)
domainLists = list(domainsReader)
for domain in domainLists:
 something = (www. + str(domain))
print(something)

 |

My program reads in a CSV file that has 500 list of domains. However, when I 
save the output of my loop to the variable name something - and later print 
something it contains only 1 entry from my csv file.
On the other hand, instead of saving of loop output to the variable something 
- if I just print, I get all 500 entries displayed.
Please advise. Thanks!

https://bpaste.net/show/3664abce17b7


   Thank you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Req Assistance with 1st Project.

2015-05-31 Thread Nym City via Tutor
Hello all,
I am working on my first Python project and I need some help. Here is what my 
project is about. I want to be able to read in from a text file that contains a 
list of IP addresses (maybe even hostnames) and write out each IP in the list 
in the following format: 172.0.0.1 OR 10.10.10.10 OR ..
After some online search, It seems like I would want to create a list (which I 
did) however, the challenge is that I can't figure out how to add the double 
quotes and OR after each entry in my list. 

Here is my current code (also attached):
f = open(BadIPList, r+)
IOCs = []
for line in f:
line = line.strip()
IOCs.append(line)
print(IOCs)
f.close()
 Thank you in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help Learn python - Step by Step

2015-05-09 Thread Nym City via Tutor
I am on the same boat. I have tried using online sites like codeacademy  and 
courses on courser but now I am starting with this new book called Automate 
The Boring Stuff with Python. So far so good. Thank you. 


 On Saturday, May 9, 2015 8:41 AM, acolta andrycolt...@gmail.com wrote:
   
 

 Hi guys,

I want to start coding in python. My background is Linux/Bash/Perl (begginner).
My appreciate if somebody will recommend books/tutorials + exercises to 
practice.

Thank you in advance,
Andrei


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


 
  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor