MoonPoint Support Logo

 


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
March
Sun Mon Tue Wed Thu Fri Sat
     
23 24 25
26 27 28 29 30 31  
2017
Months
Mar
Apr May Jun
Jul Aug Sep
Oct Nov Dec


Tue, Feb 21, 2017 9:09 pm

Viewing the list of IP address ranges for AmazonAWS

If you need to create firewall rules for outgoing connectivity to Amazon Web Services (AWS) addresses, you can obtain a complete listing of the IP address ranges that Amazon uses in JavaScript Object Notation (JSON) format from AWS IP Ranges Are Now Available in JSON Format - the download URL for the file is https://ip-ranges.amazonaws.com/ip-ranges.json

I downloaded the IP address range file from https://ip-ranges.amazonaws.com/ip-ranges.json using cURL, which is present on Apple OS X/macOS systems and is available for Linux and many other operating systems, including Microsoft Windows - see Releases and Downloads.

$ curl -o ip-ranges.json https://ip-ranges.amazonaws.com/ip-ranges.json
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   99k  100   99k    0     0   191k      0 --:--:-- --:--:-- --:--:--  191k
$

The following Python script can be used to extract just the Classless Inter-Domain Routing (CIDR) address blocks, i.e., the ip_prefix information from the JSON data stored in the ip-ranges.json file downloaded from the Amazon website and display it.

#!/usr/bin/python
import json

data = json.load(open('ip-ranges.json'))
for d in data["prefixes"]:
    print d["ip_prefix"]

[ More Info ]

[/languages/python] permanent link

Sat, Feb 11, 2017 10:27 pm

Using Python to query an SQLite database and return a count of records

I have an SQLite database that I use to track work requests on my MacBook Pro running OS X El Capitan (10.11.6) . Every week I need to determine the number of change requests I've reviewed and approved in the last week. I have been running SQL commands from a text file using sqlite3, but I wanted to start using a Python script, instead, since I want to everntually perform some additional queries as well and display the results as a web page. Initially, I had some problems displaying the record count, but found I could use cursor.fetchone() to display the count. The script I currently have consists of the following code:

#!/usr/bin/python

import sqlite3
conn = sqlite3.connect('/Users/jasmith1/Documents/Work/CRQ/CRQ.db')
cursor = conn.cursor()

sql = 'SELECT COUNT(*) FROM Tasks WHERE Approved >= date(CURRENT_DATE,"-7 day")'
cursor.execute(sql)
result = cursor.fetchone()
numApproved = result[0]
print numApproved

The first step I need to take is to import the sqlite3 module for use in Python. I then need to create a connection object for the database which I assign to conn. After that I need to create a cursor instance that I will use to execute an SQL statement. I assign the SQL statement to the variable sql (the name is arbitrary) and then execute the statement with cursor.execute(sql). The variable result is then assigned the results of cursor.fechone(). If I printed the contents of the result variable, I would see (5,), if the count was 5, since the result is a tuple with one element, but I want to see just the number, so I set numApproved to be result[0]. Instead of setting numApproved to result[0] and printing numApproved, I could skip a step and use the following two lines after sursor.execute(sql):

(numApproved,) = cursor.fetchone()
print numApproved

If I make the script excecutable with chmod 755 and then run the script, I will then see just the number of reccords approved within the last week:

$ chmod 755 weeklyApprovalCount.py
$ ./weeklyApprovalCount.py
5

References:

  1. sqlite3 — DB-API 2.0 interface for SQLite databases
    Python documentation

[/languages/python] permanent link

Sun, Nov 13, 2016 10:25 pm

Python script to create a zip file containing all files in a directory

The Python code below will create a zip file from the files and any subdirectories within a specified directory. I.e., it will recursively compress the files within a directory.

#!/usr/bin/python

# Name: zipdir.py
# Version: 1.0
# Created: 2016-11-13
# Last modified: 2016-11-13
# Purpose: Creates a zip file given a directory where the files to be zipped
# are stored and the name of the output file. Don't include the .zip extension
# when specifying the zip file name.
# Usage: zipdir.py output_filename dir_name
# Note: if the output file name and directory are not specified on the 
# command line, the script will prompt for them.

import sys, shutil

if len(sys.argv) == 1:
   dir_name = raw_input("Directory name: ")
   output_filename = raw_input("Zip file name: ")
elif len(sys.argv) == 3:
   output_filename = sys.argv[1]
   dir_name = sys.argv[2]
else:
   print "Incorrect number of arguments! Usage: zipdir.py output_filename dir_name"
   exit()

shutil.make_archive(output_filename, 'zip', dir_name)

The script takes two arguments: the output file name and the directory to be compressed. The .zip extension should not be included with the output file name; it will automatically be appended to the output file name. If no arguments are specified on the command line, the script will prompt for them. E.g., if I wanted to create a zip file named test.zip from the contents of the folder Example, I could use the command python zipdir.py test Example or, if the zipdir.py script is made executable on a Linux or Apple OS X system with chmod u+x zipdir.py, then ./zipdir.py test Example can be used.

zipdir.py

[/languages/python] permanent link

Fri, Oct 21, 2016 11:29 pm

Python script to extract a column from an Excel spreadsheet

I use the Python xlrd module to extract a column from an Excel spreadsheet. I've been using a Python script, extract-addresses.py to pull the data from a set column in an .xls spreadsheet, Directory.xls, and store it in a text file. I decided to make the script more generic, so that I can extract the data from a spreadsheet I specify when I run the script rather than a set spreadsheet and to have the specific column and output file name be variable as well. The new script is extract-column.py. The Python xlrd module will need to be installed for the script to work; xlrd can be downloaded from one of the links below:

Python Software Foundation
MoonPoint Support (version 1.0.0)

If no arguments are provided on the command line, the script will prompt for the name of the spreadsheet file from which the data should be extracted, the column number containing the data to be extracted (A is treated as column 1, B as column 2, etc.), and the name of the output file to hold the extracted data.

[ More Info ]

[/languages/python/excel] permanent link

Wed, Sep 07, 2016 9:23 pm

Unable to open X server error when attempting to run Python script

When I attempted to execute a Python script today on a CentOS Linux system from a shell prompt with ./check.py, I received the following error message:

import: unable to open X server `' @ error/import.c/ImportImageCommand/369.

The first line of code I had in the file I was attempting to run was as follows:

import json

It has been quite a while since I created a Python script, but the line looked fine to me and I couldn't understand why the JSON library wasn't being imported, but, instead, the import command seemed to be producing an error message. I believe that library has been available for use with Python since Python 2.6 and the system on which I was attempting to execute the script had Python 2.7.5 on it (you can find the version from a shell prompt by issuing the command python --version).

Eventually I realized, I had forgotten to put the shebang line as the first line in the file. The shebang line, which starts with #! tells the shell, in this case the Bash shell, which program to use to interpret the rest of the file. When I put the following line as the first line in the file, that fixed the problem.

#!/bin/python

I could have run the script successfully without that line, if I used python check.py. But when I attempted to execute the script from the command line without making it an argument to the python command, the shell was interpreting the code, rather than Python.

[/languages/python] permanent link

Wed, Jul 13, 2016 10:39 pm

Python control flow using comparators and logical operators

In Python, you can use the followng six comparators to determine if one value is or isn't equal to another or whether the value is less than, less than or equal to, greater than, or greater than or equal to another.

SymbolDescription
==Equal to
!=Not equal to
<Less than
<=Less than or equal to
>greater than
>=greater than or equal to

[ More Info ]

[/languages/python] permanent link

Wed, Jun 22, 2016 11:15 pm

Python - Checking times and dates

In Python, you can use a function called datetime.now() to retrieve the current date and time. First you need to import the datetime library so that it can be used in the script, which can be done with from datetime import datetime. You can then display the current date and time with print datetime.now(). E.g.:

$ python
Python 2.7.10 (default, Jul 14 2015, 19:46:27) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> 
>>> print datetime.now()
2016-06-22 12:34:06.719688
>>>

You can also use datetime.today().

>>> print datetime.today()
2016-06-22 14:31:26.736321
>>>

The time is in hours, minutes, seconds, and fractions of a second.

[ More Info ]

[/languages/python] permanent link

Tue, Jun 07, 2016 11:59 pm

Using Python for a simple web server

On a Mac OS X system, you can use Python to start a simple web server in any directory by opening a Terminal window (the Terminal program is found in Applications/Utilities) and entering the command python -m SimpleHTTPServer port with port being the port you wish to access the web server on. Typically, web servers listen on TCP port 80 for HTTP connections, but you can specify any currently unused port on the system with the caveat that if you pick a well-known port, i.e., a port less than 1,024, then you will need to prefix the command with sudo to run the command as root, .e.g, sudo python -m SimpleHTTPServer 80. However, you can pick ports above 1,023, such as 8080, without using sudo. E.g., python -m SimpleHTTPServer 8080. If you issued that command, you should see Python respond with "Serving HTTP on 0.0.0.0 port 8080 ..." Then, within a browser, you could access an HTML file in the directory from which you issued the command with http://localhost:8080/filename. E.g., if I wished to display a file named test.html, I could use http://localhost:8080/test.html. If test.html was just a simple HTML file, I would see GET /test.html HTTP/1.1" 200 displayed on the command line where the Python command was issued. The "200" at the end of the line is an HTTP status code indicating a successful HTTP request.

If you have a file named index.html in the directory, then just as with a normal webserver, you don't need to specify it to view its contents. E.g. http://localhost:8080 would be sufficient for displaying index.html. If there are links to images within index.html, Python would display the relevant HTTP GET commands the browser issued to retrieve the images.

When you wish to stop Python from listening on the specified port, you can use the control-C keys to stop the web server and return to the shell prompt. Note: you will see some error messages displayed when you do so.

Below is the output displayed by Python when I first accessed a file named test.html and then when I accessed the index.html file in the same directory. The index.html file had links to 4 image files contained in the same directory as the index.html file, so the GET commands issued by the browser are displayed for those as well. The 127.0.0.1 IP address is the localhost address. The last line shown was produced when I hit the control and "C" keys.

$ python -m SimpleHTTPServer 8080 Serving
HTTP on 0.0.0.0 port 8080 ...
127.0.0.1 - - [08/Jun/2016 00:00:14] "GET /test.html HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET /vlc_dmg.png HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET /vlc_internet_download.png HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET /check_album_art.png HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET /vlc_media_player.png HTTP/1.1" 200 -
127.0.0.1 - - [08/Jun/2016 00:00:32] "GET /jamendo_selections.png HTTP/1.1" 200 - 
^C

[/languages/python] permanent link

Thu, Mar 03, 2016 10:02 pm

Downloading a web page with Python using command line parameters

If you wish to download a web page with a Python script, you can imput the urllib2 module into a Python script as explained at Downloading a web page with Python. I've modified the script posted there to allow the webpage URL and output file name to be specified as command line arguments to the script:

#!/usr/bin/python

# download_page
# download a webpage to a specified file. The script takes two parameters:
# the URL of the page to download and a file name to be used to hold
# the downloaded web page.

import urllib2, sys

try:
   sys.argv[1]
except IndexError:
   print "Error - URL missing! Usage: ./download_page.py download_page_url outfile"
   sys.exit(1)
else:
   url = sys.argv[1]

try:
   sys.argv[2]
except IndexError:
   print "Error - missing output file name! Usage: ./download_page.py download_page_url outfile"
   sys.exit(1)
else:
   outfile = sys.argv[2]

page = urllib2.urlopen(url)
source = page.read()

downloadFile = open(outfile, 'w')
downloadFile.write(source)
downloadFile.close()

The sys module is imported to check the command line arguments using sys.argv[x], where x. is the number specifying the argument; sys.argv[0] is always the name of the script itself, in this case download_page.py, so sys.argv[1] should be the URL of the webpage to be saved and sys.argv[2] the file name for the output file. The file name can contain a location for the output file, e.g., mydir/somepage.html. If a directory is specified with the file name, the script doesn't check to ensure the directory exists and will exit with a Python "No such file or directory" error message should that error occur. If no directory path is included with the file name, the directory from which the script is run will be used to store the downloaded webpage.

The script will print error messages if the URL and output file name are omitted from the command line. It can be run using python ./download_page.py or ./download_page.py, if for the latter option you have first changed the file permissions on the program to mark it as executable, e.g., with chmod 755 download_page.py.

download_page.py

[/languages/python] permanent link

Fri, Jan 15, 2016 10:44 pm

Determining the number of days from or to a given date with Python

If you wish to know how many days have passed since a given date, you can do so in Python using the datetime module. If I wanted to know the number of days from December 31, 2013 until today, I could use the code below, which shows 745 days have elapsed since that date:
$ python
Python 2.7.5 (default, Jun 24 2015, 00:41:19)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import date as D
>>> print (D.today() - D(2013, 12, 31)).days
745
>>> exit()
$

If, instead, I want to know the number of days that remain until a given date, a command such as the one below, which calculates the number of days from today until April 31, 2019, could be used.

>>> print (D(2019, 4, 30) - D.today()).days
1201

Of course, the calculations don't have to be from or to today, any arbitrary day can be selected as shown in the example below, which provides the number of days between January 1, 2016 and January 1, 2019

>>> print ( D(2019, 1, 31) - D(2016, 1, 31) ).days
1096

If you would prefer to get the result from the command line, aka shell prompt, with just one command line rather than through the Python interpreter's interactive mode, a command such as the following one could be used:

$ python -c "from datetime import date as D; print ( D(2019,1,31) - D(2016,1,31) ).days"
1096
$

[/languages/python] permanent link

Once You Know, You Newegg AliExpress by Alibaba.com

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo