Tag Archives: Python

Python and SOAP / WSDL

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

#url=”http://www.webservicex.net/ConvertSpeed.asmx?WSDL” # No authentication needed
#url=”http://servername:serverport/yourcontext/your/path/F1-COUNTRY?WSDL” # No authentication needed
url=”http://servername:serverport/yourcontext/your/path/F1-COUNTRY”

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=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221; xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”&gt;
#  <soap:Body>
#    <ConvertSpeed xmlns=”http://www.webserviceX.NET/”&gt;
#      <speed>100</speed>
#      <FromUnit>kilometersPerhour</FromUnit>
#      <ToUnit>milesPerhour</ToUnit>
#    </ConvertSpeed>
#  </soap:Body>
#</soap:Envelope>”””

#body = “””<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; 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>
#</soapenv:Envelope>”””

body = “””<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:f1=”http://yourcompany.com/F1-COUNTRY.xsd”&gt;
<soapenv:Header/>
<soapenv:Body>
<f1:F1-COUNTRY dateTimeTagFormat=”xsd:strict” transactionType=”READ”>
<f1:country>US</f1:country>
</f1:F1-COUNTRY>
</soapenv:Body>
</soapenv:Envelope>”””

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

print(response.content)

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 = 192.168.xxx.xxx [your server address to accept external calls], e.g. add at the end of the file the next lines:
    [mysqld]
    bind-address = 192.168.xxx.xxx
  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("192.168.xxx.xxx", "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
    db.close()

    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 ‘192.168.xxx.xxx’ (10060)”)

DIG


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
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:

———————————
#!python3

import cx_Oracle

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

for row in cursor:
print(row)
———————————


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.”

Source: https://en.wikiquote.org/wiki/Elon_Musk


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:

Kivy

Tkinter

wxPython

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)

Anaconda

Python(x,y)


Python and BPEL OR Python only

Implementing A Rules Engine In Python