SlideShare a Scribd company logo
1 of 17
Download to read offline
Creating Agile Unix Command Line Tools With Python

(Using Subprocess, Optparse, Doctest and more)

                   Noah Gift
                  PyCon 2008
QUICK WARNING!!!



PLEASE DO NOT SATURATE THE PYCON
NETWORK WITH ICMP REQUESTS


WAIT UNTIL YOU GET HOME AND DO IT ON
YOUR NEIGHBOR’S OPEN WIRELESS ACCESS
POINT
Python Standard Library:
A command line tool’s dream come true
 Optparse: Option Handling

 Subprocess: Elegant, One Stop, System Call API

 ConfigParser: Read/Write config files

 Threading/Queue: Concurrency

 Generators: http://www.dabeaz.com/generators/Generators.pdf

 Doctest: Example Driven Testing

 Python Egg’s w/Entry Point: Simple way to install scripts
Ideas for Command Line Tools
 Pure Python:
   No systems calls, but do “Unix stuff”
 Unix Mashups
   Mix Python + System to solve new problems.
 Network Tools
 Wrapping
   Use existing tools, change the API, or behavior
The Basics: Subprocess + Optparse


    Subprocess and Optparse:
      Just like chocolate and peanut butter.
      Alone they are good, but together they make a
      tasty treat.
Subprocess: Tips for lazy people



    If you don’t care about stdout, use subprocess.call
    If you want to do something with stdout, use
    subprocess.Popen
subprocess.call Wrap
Evaluate return code, send output to /dev/null
Optparse
Optparse is easy, all the tricky stuff is solved for you
Creating a Python/Unix Mashup Tool
        Multi-Threaded Ping Sweep


  Wrap system ping command in subprocess
  Create a queue
  Spawn some threads
  It is that easy
Multi-Threaded Ping Sweep
Convert It To A Command Line Tool
Adding SNMP Discovery
(another queue, some more threads)
Doctest Example
A Pure Python Tool
  Scapy+Storm
   +Optparse
Integrating Config Files With Your Tool
Using Setuptools To Distribute Tool




Make an egg
Create an entry point
  Entry points install your script into Operating
  System script directory, “for free”
Create a --upgrade flag and integrate upgrades into
your tool by using the Python Package Index
References
This Presentation and Examples:
  http://code.noahgift.com/pycon2008
SNMP and Python:
  http://www.ibm.com/developerworks/aix/library/
  au-netsnmpnipython/
Scapy: http://www.secdev.org/projects/scapy/
Storm: https://storm.canonical.com/
Entry Points: http://peak.telecommunity.com/
DevCenter/setuptools#automatic-script-creation

More Related Content

What's hot

Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverlessDavid Przybilla
 
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...Simplilearn
 
Do you know all of Puppet?
Do you know all of Puppet?Do you know all of Puppet?
Do you know all of Puppet?Julien Pivotto
 
Packer, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec AzurePacker, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec AzureAZUG FR
 
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...Automation makes IT better! The latest and greatest features in Geppetto 3.0,...
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...Puppet
 
Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Mindfire Solutions
 
Monitoring MySQL with Prometheus, Grafana and Percona Dashboards
Monitoring MySQL with Prometheus, Grafana and Percona DashboardsMonitoring MySQL with Prometheus, Grafana and Percona Dashboards
Monitoring MySQL with Prometheus, Grafana and Percona DashboardsJulien Pivotto
 
Avoiding surprises with Chef and Vagrant
Avoiding surprises with Chef and VagrantAvoiding surprises with Chef and Vagrant
Avoiding surprises with Chef and Vagrantandygale
 
OpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in PythonOpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in PythonCodeOps Technologies LLP
 
Ansible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOTAnsible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOTkeerthi124
 
Advance discussion on Ansible - Rahul Inti
Advance discussion on Ansible - Rahul IntiAdvance discussion on Ansible - Rahul Inti
Advance discussion on Ansible - Rahul IntiSahil Davawala
 
ZLM-Cython Build you first module
ZLM-Cython Build you first moduleZLM-Cython Build you first module
ZLM-Cython Build you first moduleVladimir Ulogov
 
Creating Perl modules with Dist::Zilla
Creating Perl modules with Dist::ZillaCreating Perl modules with Dist::Zilla
Creating Perl modules with Dist::ZillaMark Gardner
 
Intro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupIntro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupRamesh Godishela
 
Contribute to rails
Contribute to railsContribute to rails
Contribute to railsmartinsvalin
 
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...Chris Fregly
 

What's hot (20)

Python in the land of serverless
Python in the land of serverlessPython in the land of serverless
Python in the land of serverless
 
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
Ansible Tutorial For Beginners | What Is Ansible And How It Works? | Ansible ...
 
Do you know all of Puppet?
Do you know all of Puppet?Do you know all of Puppet?
Do you know all of Puppet?
 
Packer, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec AzurePacker, Terraform, Ansible avec Azure
Packer, Terraform, Ansible avec Azure
 
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...Automation makes IT better! The latest and greatest features in Geppetto 3.0,...
Automation makes IT better! The latest and greatest features in Geppetto 3.0,...
 
Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1Asynchronous Programming in C# - Part 1
Asynchronous Programming in C# - Part 1
 
Monitoring MySQL with Prometheus, Grafana and Percona Dashboards
Monitoring MySQL with Prometheus, Grafana and Percona DashboardsMonitoring MySQL with Prometheus, Grafana and Percona Dashboards
Monitoring MySQL with Prometheus, Grafana and Percona Dashboards
 
Avoiding surprises with Chef and Vagrant
Avoiding surprises with Chef and VagrantAvoiding surprises with Chef and Vagrant
Avoiding surprises with Chef and Vagrant
 
OpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in PythonOpenWhisk by Example - Auto Retweeting Example in Python
OpenWhisk by Example - Auto Retweeting Example in Python
 
Ansible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOTAnsible training | redhat Ansible 2.5 Corporate course - GOT
Ansible training | redhat Ansible 2.5 Corporate course - GOT
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Advance discussion on Ansible - Rahul Inti
Advance discussion on Ansible - Rahul IntiAdvance discussion on Ansible - Rahul Inti
Advance discussion on Ansible - Rahul Inti
 
2021laravelconftwslides6
2021laravelconftwslides62021laravelconftwslides6
2021laravelconftwslides6
 
Power Of Zero
Power Of ZeroPower Of Zero
Power Of Zero
 
zlm-cython
zlm-cythonzlm-cython
zlm-cython
 
ZLM-Cython Build you first module
ZLM-Cython Build you first moduleZLM-Cython Build you first module
ZLM-Cython Build you first module
 
Creating Perl modules with Dist::Zilla
Creating Perl modules with Dist::ZillaCreating Perl modules with Dist::Zilla
Creating Perl modules with Dist::Zilla
 
Intro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetupIntro to-ansible-sep7-meetup
Intro to-ansible-sep7-meetup
 
Contribute to rails
Contribute to railsContribute to rails
Contribute to rails
 
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...
Swift for TensorFlow - Tanmay Bakshi - Advanced Spark and TensorFlow Meetup -...
 

Viewers also liked

Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)open-e
 
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
Red Hat Storage Day Seattle: Why Software-Defined Storage MattersRed Hat Storage Day Seattle: Why Software-Defined Storage Matters
Red Hat Storage Day Seattle: Why Software-Defined Storage MattersRed_Hat_Storage
 
Swift Architecture and Practice, by Alex Yang
Swift Architecture and Practice, by Alex YangSwift Architecture and Practice, by Alex Yang
Swift Architecture and Practice, by Alex YangHui Cheng
 
Building RESTful APIs
Building RESTful APIsBuilding RESTful APIs
Building RESTful APIsSilota Inc.
 
Software Defined presentation
Software Defined presentationSoftware Defined presentation
Software Defined presentationJohn Rhodes
 
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerDhilipsiva DS
 
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011Stephen Foskett
 
Mirantis OpenStack-DC-Meetup 17 Sept 2014
Mirantis OpenStack-DC-Meetup 17 Sept 2014Mirantis OpenStack-DC-Meetup 17 Sept 2014
Mirantis OpenStack-DC-Meetup 17 Sept 2014Mirantis
 
REST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIsREST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIsHiranya Jayathilaka
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Innovecs
 
Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyAlessandro Cucci
 
Open stack architecture overview-meetup-6-6_2013
Open stack architecture overview-meetup-6-6_2013Open stack architecture overview-meetup-6-6_2013
Open stack architecture overview-meetup-6-6_2013Mirantis
 
OpenStack keystone identity service
OpenStack keystone identity serviceOpenStack keystone identity service
OpenStack keystone identity serviceopenstackindia
 
Software-Defined Storage (SDS)
Software-Defined Storage (SDS)Software-Defined Storage (SDS)
Software-Defined Storage (SDS)Ali Mirfallah
 
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...LF Events
 
Anatomy of neutron from the eagle eyes of troubelshoorters
Anatomy of neutron from the eagle eyes of troubelshoortersAnatomy of neutron from the eagle eyes of troubelshoorters
Anatomy of neutron from the eagle eyes of troubelshoortersSadique Puthen
 

Viewers also liked (20)

Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
Open-E DSS V7 Active-Active Load Balanced iSCSI HA Cluster (with bonding)
 
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
Red Hat Storage Day Seattle: Why Software-Defined Storage MattersRed Hat Storage Day Seattle: Why Software-Defined Storage Matters
Red Hat Storage Day Seattle: Why Software-Defined Storage Matters
 
Swift Architecture and Practice, by Alex Yang
Swift Architecture and Practice, by Alex YangSwift Architecture and Practice, by Alex Yang
Swift Architecture and Practice, by Alex Yang
 
Building RESTful APIs
Building RESTful APIsBuilding RESTful APIs
Building RESTful APIs
 
Software Defined presentation
Software Defined presentationSoftware Defined presentation
Software Defined presentation
 
Microservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and DockerMicroservices with Swagger, Flask and Docker
Microservices with Swagger, Flask and Docker
 
Core Concept: Software Defined Everything
Core Concept: Software Defined EverythingCore Concept: Software Defined Everything
Core Concept: Software Defined Everything
 
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
"FCoE vs. iSCSI - Making the Choice" from Interop Las Vegas 2011
 
Mirantis OpenStack-DC-Meetup 17 Sept 2014
Mirantis OpenStack-DC-Meetup 17 Sept 2014Mirantis OpenStack-DC-Meetup 17 Sept 2014
Mirantis OpenStack-DC-Meetup 17 Sept 2014
 
REST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIsREST Coder: Auto Generating Client Stubs and Documentation for REST APIs
REST Coder: Auto Generating Client Stubs and Documentation for REST APIs
 
Rest in flask
Rest in flaskRest in flask
Rest in flask
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
 
SDN Abstractions
SDN AbstractionsSDN Abstractions
SDN Abstractions
 
Rest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemyRest API using Flask & SqlAlchemy
Rest API using Flask & SqlAlchemy
 
Nginx in production
Nginx in productionNginx in production
Nginx in production
 
Open stack architecture overview-meetup-6-6_2013
Open stack architecture overview-meetup-6-6_2013Open stack architecture overview-meetup-6-6_2013
Open stack architecture overview-meetup-6-6_2013
 
OpenStack keystone identity service
OpenStack keystone identity serviceOpenStack keystone identity service
OpenStack keystone identity service
 
Software-Defined Storage (SDS)
Software-Defined Storage (SDS)Software-Defined Storage (SDS)
Software-Defined Storage (SDS)
 
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...
Learning From Real Practice of Providing Highly Available Hybrid Cloud Servic...
 
Anatomy of neutron from the eagle eyes of troubelshoorters
Anatomy of neutron from the eagle eyes of troubelshoortersAnatomy of neutron from the eagle eyes of troubelshoorters
Anatomy of neutron from the eagle eyes of troubelshoorters
 

Similar to Pycon 2008: Python Command-line Tools *Nix

Python Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txtPython Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txtInexture Solutions
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfssuser8b3cdd
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to pythonMohammed Rafi
 
Intro to Programming Week 2_Python Installation.pptx
Intro to Programming Week 2_Python Installation.pptxIntro to Programming Week 2_Python Installation.pptx
Intro to Programming Week 2_Python Installation.pptxiksanbukhori
 
The magic of IPython Notebook
The magic of IPython NotebookThe magic of IPython Notebook
The magic of IPython NotebookAlexey Agapov
 
Windows PowerShell - Billings .NET User Group - August 2009
Windows PowerShell - Billings .NET User Group - August 2009Windows PowerShell - Billings .NET User Group - August 2009
Windows PowerShell - Billings .NET User Group - August 2009John Clayton
 
How to create a basic website with Python on Django
How to create a basic website with Python on DjangoHow to create a basic website with Python on Django
How to create a basic website with Python on DjangoArmağan Ersöz
 
Getting to push_button_deploys
Getting to push_button_deploysGetting to push_button_deploys
Getting to push_button_deploysChristian Mague
 
Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python Jose Manuel Ortega Candel
 
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...Puppet
 
Training report 1923-b.e-eee-batchno--intern-54 (1).pdf
Training report 1923-b.e-eee-batchno--intern-54 (1).pdfTraining report 1923-b.e-eee-batchno--intern-54 (1).pdf
Training report 1923-b.e-eee-batchno--intern-54 (1).pdfYadavHarshKr
 
Pywps a tutorial for beginners and developers
Pywps a tutorial for beginners and developersPywps a tutorial for beginners and developers
Pywps a tutorial for beginners and developersJorge Mendes
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python ProgrammingDozie Agbo
 
Seminar report On Python
Seminar report On PythonSeminar report On Python
Seminar report On PythonShivam Gupta
 
TechWiseTV Workshop: Catalyst Switching Programmability
TechWiseTV Workshop: Catalyst Switching ProgrammabilityTechWiseTV Workshop: Catalyst Switching Programmability
TechWiseTV Workshop: Catalyst Switching ProgrammabilityRobb Boyd
 
Seminar report on python 3 course
Seminar report on python 3 courseSeminar report on python 3 course
Seminar report on python 3 courseHimanshuPanwar38
 
Evented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPEvented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPmarkstory
 
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdf
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdfREPORT ON AUDIT COURSE PYTHON BY SANA 2.pdf
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdfSana Khan
 

Similar to Pycon 2008: Python Command-line Tools *Nix (20)

Pyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdfPyhton-1a-Basics.pdf
Pyhton-1a-Basics.pdf
 
Python Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txtPython Requirements File How to Create Python requirements.txt
Python Requirements File How to Create Python requirements.txt
 
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdfThe Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
The Python Book_ The ultimate guide to coding with Python ( PDFDrive ).pdf
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Intro to Programming Week 2_Python Installation.pptx
Intro to Programming Week 2_Python Installation.pptxIntro to Programming Week 2_Python Installation.pptx
Intro to Programming Week 2_Python Installation.pptx
 
The magic of IPython Notebook
The magic of IPython NotebookThe magic of IPython Notebook
The magic of IPython Notebook
 
Elasticwulf Pycon Talk
Elasticwulf Pycon TalkElasticwulf Pycon Talk
Elasticwulf Pycon Talk
 
Windows PowerShell - Billings .NET User Group - August 2009
Windows PowerShell - Billings .NET User Group - August 2009Windows PowerShell - Billings .NET User Group - August 2009
Windows PowerShell - Billings .NET User Group - August 2009
 
How to create a basic website with Python on Django
How to create a basic website with Python on DjangoHow to create a basic website with Python on Django
How to create a basic website with Python on Django
 
Getting to push_button_deploys
Getting to push_button_deploysGetting to push_button_deploys
Getting to push_button_deploys
 
Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python Tips and tricks for data science projects with Python
Tips and tricks for data science projects with Python
 
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
PuppetConf 2016: Deploying Multi-Tier Windows Applications with Application O...
 
Training report 1923-b.e-eee-batchno--intern-54 (1).pdf
Training report 1923-b.e-eee-batchno--intern-54 (1).pdfTraining report 1923-b.e-eee-batchno--intern-54 (1).pdf
Training report 1923-b.e-eee-batchno--intern-54 (1).pdf
 
Pywps a tutorial for beginners and developers
Pywps a tutorial for beginners and developersPywps a tutorial for beginners and developers
Pywps a tutorial for beginners and developers
 
First Steps in Python Programming
First Steps in Python ProgrammingFirst Steps in Python Programming
First Steps in Python Programming
 
Seminar report On Python
Seminar report On PythonSeminar report On Python
Seminar report On Python
 
TechWiseTV Workshop: Catalyst Switching Programmability
TechWiseTV Workshop: Catalyst Switching ProgrammabilityTechWiseTV Workshop: Catalyst Switching Programmability
TechWiseTV Workshop: Catalyst Switching Programmability
 
Seminar report on python 3 course
Seminar report on python 3 courseSeminar report on python 3 course
Seminar report on python 3 course
 
Evented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHPEvented applications with RabbitMQ and CakePHP
Evented applications with RabbitMQ and CakePHP
 
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdf
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdfREPORT ON AUDIT COURSE PYTHON BY SANA 2.pdf
REPORT ON AUDIT COURSE PYTHON BY SANA 2.pdf
 

More from Lecturer UC Davis & Northwestern (8)

Sentiment analysis for Business Analytics
Sentiment analysis for Business AnalyticsSentiment analysis for Business Analytics
Sentiment analysis for Business Analytics
 
Social power and_influence_in_the_nba_gitpro
Social power and_influence_in_the_nba_gitproSocial power and_influence_in_the_nba_gitpro
Social power and_influence_in_the_nba_gitpro
 
Erlang factory slides
Erlang factory slidesErlang factory slides
Erlang factory slides
 
iphone and Google App Engine
iphone and Google App Engineiphone and Google App Engine
iphone and Google App Engine
 
Favorite X Code4 Features
Favorite X Code4 FeaturesFavorite X Code4 Features
Favorite X Code4 Features
 
RabbitMQ Plugins Talk
RabbitMQ Plugins TalkRabbitMQ Plugins Talk
RabbitMQ Plugins Talk
 
PyCon 2011: IronPython Command Line
PyCon 2011:  IronPython Command LinePyCon 2011:  IronPython Command Line
PyCon 2011: IronPython Command Line
 
iphonedevcon 2010: Cooking with iAd
iphonedevcon 2010:  Cooking with iAd iphonedevcon 2010:  Cooking with iAd
iphonedevcon 2010: Cooking with iAd
 

Pycon 2008: Python Command-line Tools *Nix

  • 1. Creating Agile Unix Command Line Tools With Python (Using Subprocess, Optparse, Doctest and more) Noah Gift PyCon 2008
  • 2. QUICK WARNING!!! PLEASE DO NOT SATURATE THE PYCON NETWORK WITH ICMP REQUESTS WAIT UNTIL YOU GET HOME AND DO IT ON YOUR NEIGHBOR’S OPEN WIRELESS ACCESS POINT
  • 3. Python Standard Library: A command line tool’s dream come true Optparse: Option Handling Subprocess: Elegant, One Stop, System Call API ConfigParser: Read/Write config files Threading/Queue: Concurrency Generators: http://www.dabeaz.com/generators/Generators.pdf Doctest: Example Driven Testing Python Egg’s w/Entry Point: Simple way to install scripts
  • 4. Ideas for Command Line Tools Pure Python: No systems calls, but do “Unix stuff” Unix Mashups Mix Python + System to solve new problems. Network Tools Wrapping Use existing tools, change the API, or behavior
  • 5. The Basics: Subprocess + Optparse Subprocess and Optparse: Just like chocolate and peanut butter. Alone they are good, but together they make a tasty treat.
  • 6. Subprocess: Tips for lazy people If you don’t care about stdout, use subprocess.call If you want to do something with stdout, use subprocess.Popen
  • 7. subprocess.call Wrap Evaluate return code, send output to /dev/null
  • 8. Optparse Optparse is easy, all the tricky stuff is solved for you
  • 9. Creating a Python/Unix Mashup Tool Multi-Threaded Ping Sweep Wrap system ping command in subprocess Create a queue Spawn some threads It is that easy
  • 11. Convert It To A Command Line Tool
  • 12. Adding SNMP Discovery (another queue, some more threads)
  • 14. A Pure Python Tool Scapy+Storm +Optparse
  • 15. Integrating Config Files With Your Tool
  • 16. Using Setuptools To Distribute Tool Make an egg Create an entry point Entry points install your script into Operating System script directory, “for free” Create a --upgrade flag and integrate upgrades into your tool by using the Python Package Index
  • 17. References This Presentation and Examples: http://code.noahgift.com/pycon2008 SNMP and Python: http://www.ibm.com/developerworks/aix/library/ au-netsnmpnipython/ Scapy: http://www.secdev.org/projects/scapy/ Storm: https://storm.canonical.com/ Entry Points: http://peak.telecommunity.com/ DevCenter/setuptools#automatic-script-creation