Python – virtualenv / requests

Python – virtualenv / requests

$ cd c:\py\session4
$ python -m venv venv [If installed Python 3] [Create virtual environment named venv]
[USE WITH CARE!] $ virtualenv venv [If installed Python older than 3.4] [Create virtual environment]
$ source venv/bin/activate [activate virtual environment ](Linux)
$ venv\Scripts\activate [activate virtual environment ] (Windows)
(venv) c:\py\session4> pip install –proxy http://your-proxy:your-port requests
(venv) c:\py\session4> python -m idlelib.idle
>>> import requests
(venv) c:\py\session4> deactivate [deactivate virtual environment ]

Python – requests

>>> import requests
>>> proxies = {
‘http’: ‘http://your-proxy:your-port’,
‘https’: ‘http://your-proxy:your-port’,
>>> requests.get(‘’, proxies=proxies)
>>> r = requests.get(‘’, proxies=proxies)
>>> r.text
(Squeezed text (1071 lines).)
>>> r.encoding
>>> r.content
(Squeezed text (1072 lines).)
>>> r.json()
[{‘id’: ‘11281251877’, ‘type’: ‘IssueCommentEvent’, ‘actor’: {‘id’: 48688167, ‘login’: ‘bykoi’, ‘display_login’: ‘bykoi’…
>>> r.status_code

Python and SOAP / WSDL

# SOAP/WSDL — This is a Python 2.7 version of a sample code consuming a Web Service
# ==========
import requests

#url=”” # No authentication needed
#url=”http://servername:serverport/yourcontext/your/path/F1-COUNTRY?WSDL” # No authentication needed

headers = {‘content-type’: ‘text/xml’}
#headers = {‘content-type’: ‘text/xml’,
#           ‘Authorization’: ‘Basic blablabla==’}
#headers = {‘content-type’: ‘text/xml’,
#           ‘Authorization’: ‘Basic blublublu==’}

proxies = {“http”: “http://proxy:port”,
“https”: “https://proxy:port”,
“socks”: “socks://proxy:port”,
“ftp”: “ftp://proxy:port”}

#body = “””<?xml version=”1.0″ encoding=”utf-8″?>
#<soap:Envelope xmlns:xsi=”; xmlns:xsd=”; xmlns:soap=””&gt;
#  <soap:Body>
#    <ConvertSpeed xmlns=”http://www.webserviceX.NET/”&gt;
#      <speed>100</speed>
#      <FromUnit>kilometersPerhour</FromUnit>
#      <ToUnit>milesPerhour</ToUnit>
#    </ConvertSpeed>
#  </soap:Body>

#body = “””<soapenv:Envelope xmlns:soapenv=”; xmlns:web=”http://www.webserviceX.NET/”&gt;
#   <soapenv:Header/>
#   <soapenv:Body>
#      <web:ConvertSpeed>
#         <web:speed>100</web:speed>
#         <web:FromUnit>kilometersPerhour</web:FromUnit>
#         <web:ToUnit>milesPerhour</web:ToUnit>
#      </web:ConvertSpeed>
#   </soapenv:Body>

body = “””<soapenv:Envelope xmlns:soapenv=”; xmlns:f1=””&gt;
<f1:F1-COUNTRY dateTimeTagFormat=”xsd:strict” transactionType=”READ”>

#response =,data=body,headers=headers)
response =,data=body,headers=headers, auth=(‘user’, ‘pass’))  # no proxies  – used in intranet
#response =,data=body,headers=headers,proxies=proxies)  # used in extranet
#response =,data=body,headers=headers,proxies=proxies,auth=(‘user’, ‘pass’))


Python and MySQL

If an external application, e.g. written in Python, needs to access MySQL server, take a look at MySQL Option Setting  bind-address . You can also see some practical advise in Ubuntu online help about MySQL. For MySQL Options File format see MySQL Reference Manual.

Ubuntu Machine Steps:

  1. Edit sudo vi /etc/mysql/my.cnf and modify the line with bind-address = [your server address to accept external calls], e.g. add at the end of the file the next lines:
    bind-address =
  2. Restart MySQL daemon:
    sudo service mysql restart

Python Remote Machine Steps:

  1. Proceed as in the tutorialspointPython MySQL Database Access” article, e.g.:
    import MySQLdb
    print "Hello World!"
    # Open database connection
    db = MySQLdb.connect("", "testuser", "test123", "testdb" )
    # prepare a cursor object using cursor() method
    cursor = db.cursor()
    # execute SQL query using execute() method.
    cursor.execute("SELECT VERSION()")
    # Fetch a single row using fetchone() method.
    data = cursor.fetchone()
    print "Database version : %s " % data
    # disconnect from server

    Output Sample:

    Hello World!
    Database version : 5.7.15-0ubuntu0.16.04.1

    Possible output in case there is no server running:

    Hello World!
    Traceback (most recent call last):

    _mysql_exceptions.OperationalError: (2003, “Can’t connect to MySQL server on ‘’ (10060)”)


Additional Tutorials Links

MySQL Python tutorial


Some Python Articles and Links I Like

The Mastering Oracle+Python Series (by Przemyslaw Piotrowski)

Mastering Oracle+Python, Part 7: Service-Oriented Python Architecture

Exploring OBIEE Web Services through Python

“(…)January 29th, 2014 by Robin Moffatt
My very first blog post for Rittman Mead was on the subject of Web Services in BI Publisher 11g, and now I want to return to the subject here, looking at the Web Services of OBIEE itself.
Web Services are basically a way of enabling an application or service to offer up an API to as-yet-undefined-clients. This may be an API for fetching data, sending data, or invoking a service or action. In this age of The Cloud, systems being able to interact in this abstracted manner (i.e. without requiring one to be aware of the other at design time) is a Good Thing and something that if an application does not support is rather frowned upon.
For a simple explanation of Web Services, and details of how to explore and test them using SoapUI then refer to my article Web Services in BI Publisher 11g.
OBIEE itself has supported Web Services for a long time now, but they are exposed through the SOAP, a HTTP-based protocol that relies on carefully formed XML messages. This is fine, but had meant that to actually utilise them beyond tinkering within SoapUI you had to create a heavy-weight JDeveloper project or similar. One of the many great things about working at Rittman Mead is that as a company we use Macs, meaning that a unix-like command line, and Python, is just a click away. Recently I discovered that Python has a library called SUDS. This makes calling a web service as simple as:
from suds.client import Client client = Client(‘http://obiee-server:9704/analytics-ws/saw.dll/wsdl/v7&#8242;) sessionid = client.service[‘SAWSessionService’].logon(‘weblogic’,’Password01′)
With this code I have just logged into OBIEE and returned a session ID token that I can now use in subsequent web service calls. In the background, SUDS sorts out the forming of the XML SOAP messages to send to the web service, and the parsing of the returned XML SOAP message into a Python object matching the object. So, now I can actually start exploring and programming using the Web Services straight from my command line…nice.
The Web Services discussed below are all session based, meaning that you have to first authenticate and retrieve a session ID, which is then used for subsequent Web Service calls.(…)”

Soap call in Python

Python : Soap using requests

Requests: HTTP for Humans

What SOAP client libraries exist for Python, and where is the documentation for them? [closed]

Suds is a lightweight SOAP python client for consuming Web Services.

Zeep: Python SOAP client

Using Python to connect to Oracle DB, and extract results to CSV” from “JK Vine Consulting Blog

Accessing Oracle Database data from Python (sample)

Based on:
Mastering Oracle+Python, Part 1: Querying Best Practices

This is a very basic sample in Python script accessing Oracle database:


import cx_Oracle

dsn_tns = cx_Oracle.makedsn(‘hostname’, db_port, ‘sid’)
db = cx_Oracle.connect(‘user’, ‘passwd’, dsn_tns)

for row in cursor:

Python and SQLite

SQLite Tutorial

DB Browser for SQLite

Python business intelligence (PyData 2012 talk)

Python Intro For Managers

>> D = {‘EN’: ‘Experimenting with the use of Python (programming language) as a universal tool for software programming.’, ‘ES’:’Experimentando con el uso de Python (lenguaje de programación) como herramienta universal para la programación de software.’}
>>> print(D)
{‘EN’: ‘Experimenting with the use of Python (programming language) as a universal tool for software programming.’, ‘ES’: ‘Experimentando con el uso de Python (lenguaje de programación) como herramienta universal para la programación de software.’}

From Jeff Knupp Blog:

Improve Your Python: Python Classes and Object Oriented Programming

Open Sourcing a Python Project the Right Way

Python & Java: A Side-by-Side Comparison

Python is used as prototyping tool.

“Everything works in PowerPoint; but if you have the physical item or some demonstration software, that’s much more convincing to people than a PowerPoint presentation or a business plan.”


Instant Python

A Byte of Python

The Ancient Art of the Numerati

Automate the Boring Stuff with Python

Python 3 Module of the Week

Web Frameworks:

Twisted (software)

Flask (web framework)

Django (web framework)

Multi-platform GUI frameworks:




For a Python and Glade tutorial see the article “Creating a GUI using PyGTK and Glade” (orig).

Windows Compilers

Unofficial Windows Binaries for Python Extension Packages

Some Python Distributions useful for Beginners

Enthought Python Distribution (EPD)



Python and BPEL OR Python only

Implementing A Rules Engine In Python