Generating a WSDL using Python and SOAPpy - python

First of all, I will admit I am a novice to web services, although I'm familiar with HTML and basic web stuff. I created a quick-and-dirty web service using Python that calls a stored procedure in a MySQL database, that simply returns a BIGINT value. I want to return this value in the web service, and I want to generate a WSDL that I can give our web developers. I might add that the stored procedure only returns one value.
Here's some example code:
#!/usr/bin/python
import SOAPpy
import MySQLdb
def getNEXTVAL():
cursor = db.cursor()
cursor.execute( "CALL my_stored_procedure()" ) # Returns a number
result=cursor.fetchall()
for record in result:
return record[0]
db=MySQLdb.connect(host="localhost", user="myuser", passwd="********", db="testing")
server = SOAPpy.SOAPServer(("10.1.22.29", 8080))
server.registerFunction(getNEXTVAL)
server.serve_forever()
I want to generate a WSDL that I can give to the web folks, and I'm wondering if it's possible to have SOAPpy just generate one for me. Is this possible?

When I tried to write Python web service last year, I ended up using ZSI-2.0 (which is something like heir of SOAPpy) and a paper available on its web.
Basically I wrote my WSDL file by hand and then used ZSI stuff to generate stubs for my client and server code. I wouldn't describe the experience as pleasant, but the application did work.

I want to generate a WSDL that I can give to the web folks, ....
You can try soaplib. It has on-demand WSDL generation.

Sorry for the question few days ago. Now I can invoke the server successfully. A demo is provided:
def test_soappy():
"""test for SOAPpy.SOAPServer
"""
#okay
# it's good for SOAPpy.SOAPServer.
# in a method,it can have morn than 2 ws server.
server = SOAPProxy("http://localhost:8081/")
print server.sum(1,2)
print server.div(10,2)

Related

API Key Authentication in Elasticsearch with python

I was hoping to find an answer to my problem with the elasticsearch python framework. Maybe I'm completely blind or doing something absolutely wrong, but I'm very confused right now and can't find an adequate answer.
I'm currently trying to establish a connection to my elastic search API using the elasticsearch python framework, my code looks like this:
from elasticsearch import Elasticsearch
def create_es_connection(host: str, port: int, api_key_id: str, api_key: str, user: str, pw: str) -> Elasticsearch:
return Elasticsearch([f"https://{user}:{pw}#{host}:{port}"])
This is working fine. I then created an API key for my testuser, which I'm giving to the function above as well. I am now trying to do something similar like this: Elasticsearch([f"https://{api_key_id}:{api_key}#{host}:{port}"]) so, I want to leave out the user and password completely, because in regards to the greater project behind this snippet, I'm not feeling very well in saving the user/password credentials in my project (and maybe even pushing it to our git server). Sooner or later, these credentials have to be entered somewhere and I was thinking that only saving the API key and to authenticate with that could be more safe. Unfortunately, this isn't working out like I planned and I couldn't find anything about how to authenticate with the API key only.
What am I doing wrong? In fact, I found so little about this, that I'm questioning my fundamental understanding here. Thank you for your replies!
working configuration for me was :
es = Elasticsearch(['localhost:9200'], api_key=('DuSkVm8BZ5TMcIF99zOC','2rs8yN26QSC_uPr31R1KJg'))
Elasticsearch's documentation shows how to generate and use (at the bottom of the page) an API key: https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-api-key.html
curl -H "Authorization: ApiKey ..." http://localhost:9200/_cluster/health
-H means "header", so to do the same in Python you will need to set this header. Rummaging through the elasticsearch module source code tells me that you might just be able to do the following:
Elasticsearch([f'http://{host}:{port}'], api_key=api_key)
The reason for this is that **kwargs of the Elasticsearch.__init__ method are passed to Transport.__init__, the **kwargs of that are passed to Connection.__init__, which takes an api_key arg which is then used in
_get_api_key_header_val to construct the appropriate header.
The following code shows that the api key header gets added to the HTTP headers of the request:
import elasticsearch, logging, http.client
http.client.HTTPConnection.debuglevel = 5
logging.basicConfig(level=logging.DEBUG)
c = elasticsearch.Elasticsearch(['localhost'], api_key='TestApiKey')
print(c.cluster.health(wait_for_status='green'))
This is definitely something that should be added to Elasticsearch's docs.

Connecting to Oracle AQ with Python

I am trying to connect to an Oracle advanced queue using Python.
The basic principle of what I am trying to do is this: A queue has been set up that will send a message once every hour, I want to deque this message and analyze it with some code I've written.
I have credentials (host, port, sid, user & passwn) but I am not sure how to set up the connection and start consuming.
From what I can understand from previous questions on the web the cx_oracle module should have capabilities to do this, but I cannot figure out how to do this in practice.
If you have any links to tutorials that shows how this is done, or if you have some sample code yourself It would be highly appreciated. I have some experiece with RabbitMQ queues but it seems that there is alot less examples and tutorials for Oracle AQ, hence my question here.
The cx_Oracle Advanced Queuing docs are here.
An example would be something like:
# setup connection
connection = cx_Oracle.Connection('connection string')
# get the options
options = connection.deqoptions()
# set relevant options:
options.navigation = cx_Oracle.DEQ_FIRST_MSG
options.wait = cx_Oracle.DEQ_WAIT_FOREVER
# continuously deque
while connection.deq(NAME_OF_QUEUE, options, messageProperties, payload):
print(payload)
Anthony Tuininga (cx_Oracle author) has a much more complete example on Github.

Backend for SOAP in Python

I am a Python programmer, but new to webservices.
Task:
I have a Typo3-Frontend and a Postgresql-database. I want to write a backend between these two parts in Python. Another developer gave me a wsdl-file and xsd-file to work with, so we use SOAP. The program I code should be bound to a port (TCP/IP) and act as a service. The data/payload will be encoded in json-objects.
Webclient <---> Frontend <---> Backend(Me) <---> Database
My ideas:
I code all functions by hand from the wsdl-file, with the datatypes from xsd.
I bind a service to a port that recieves incoming json-data
I parse the incoming data, do some database-operations, do other stuff
I return the result to the frontend.
Questions:
Do I have to code all the methods/functions descripted in the wsdl-file by hand?
Do I have to define the complex datatypes by hand?
How should I implement the communication between the frontend and the backend?
Thanks in advance!
Steffen
I have successfully used suds client to communicate with Microsoft Dynamics NAV (former Navision).
Typical session looks like this:
from suds.client import Client
url = 'http://localhost:7080/webservices/WebServiceTestBean?wsdl'
client = Client(url)
By issuing print client you get the list of types and operations supported by the serivce.
Suds - version: 0.3.3 build: (beta) R397-20081121
Service (WebServiceTestBeanService) tns="http://test.server.enterprise.rhq.org/"
Prefixes (1):
ns0 = "http://test.server.enterprise.rhq.org/"
Ports (1):
(Soap)
Methods:
addPerson(Person person, )
echo(xs:string arg0, )
getList(xs:string str, xs:int length, )
getPercentBodyFat(xs:string name, xs:int height, xs:int weight)
getPersonByName(Name name, )
hello()
testExceptions()
testListArg(xs:string[] list, )
testVoid()
updatePerson(AnotherPerson person, name name, )
Types (23):
Person
Name
Phone
AnotherPerson
WSDL operations are exposed as ordinary python functions and you can use ordinary dicts in place of WSDL types.
I would go with Twisted since I am working with it anyway and enjoy the system.
Another asynchronous option may be Tornado.
Or a synchronous version with Flask.
I'm sure there are many other options. I would look for a higher level framework like those listed above so you don't have to spend too much time connecting the frontend to the backend.
You can use Python libraries as SOAPpy or PyWS.

How to expose data to zabbix

Here is my goal: I would like to be able to report various metrics to zabbix so that we can display the graphs on a web page.
These metrics include:
latency per soap service submission
various query results from one or more databases.
What things do I need to write and/or expose? Or is the zabbix server going to go and get it from an exposed service somewhere?
I've been advised that a script that returns a single value will work, but I'm wondering if that's the right way.
I can offer 2 suggestions to get the metrics into Zabbix:
Use the zabbix_sender binary to feed the data from your script directly to the Zabbix server. This allows your script to call on it's own interval and set all the parameters needed. You really only need to know the location to the zabbix_sender binary. Inside the Zabbix server interface, you would create items with the type of Zabbix trapper. This is the item type which receives values send from the zabbix_sender. You make up the key name and it has to match.
The second way you could do this is to specify a key name and script/binary inside the zabbix_agentd.conf file. Every time the Zabbix server requests this item the script would be called and the data from the script recorded. This allows you to set the intervals in the Zabbix item configuration rather than forcing you to run your script on its own intervals. However, you would need to add this extra bit of information to your zabbix_agentd.conf file for every host.
There may be other ways to do this directly from Python (zabbix_sender bindings for Python maybe?). But these are the 2 ways I have used before which work well. This isn't really Python specific. But you should be able to use zabbix_sender in your Python scripting. Hope this information helps!
Update: I also remembered that Zabbix was working on/has a API (JSON/RPC style). But the documentation site is down at the moment and I am not sure if the API is for submitting item data or not. Here is the Wiki on the API: http://www.zabbix.com/wiki/doc/api
And a project for Python API: https://github.com/gescheit/scripts/tree/master/zabbix/
There seems to be little documentation on the API as it is new as of Zabbix version 1.8
Actually there is a python binding for zabbix_sender. http://pypi.python.org/pypi/zbxsend

Django Can't Read Membase Data?

I have two Membase clients - a Django website and a small python test file. I observed that the data saved by the python file can't be seen by the Django website, it returns None. However, If I save the same data (same keys and values) in the Django website, the site will be able to read the keys/values then.
This is the same code I use in both clients:
import memcache
mc = memcache.Client(['127.0.0.1:11211'])
ids = ["100000314125574", "100000335352069"]
users = mc.get_multi(ids)
I am using the python-memcache client library.
I have been trying to solve this for hours. Please share your ideas. It is a real mystery to me why Django can't read the data saved from another client.
A good troubleshooting step is just to telnet manually to 127.0.0.1 on port 11211 and do a 'get ' for that key. If you get something back then at least you know the Membase server is functioning properly and you can focus on the Django side of things...where I'm much less knowledgeable.

Categories