Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
3.4. Python AMQP 1.0 Client API
3.4.1. Getting Started with Qpid Proton Python Client
3.4.1.1. Introduction to Qpid Proton Python Client
What is Qpid Proton
Introduction to the Qpid Proton Python Client
 Container(name_of_a_handler_class(arguments)).run()
					- Where the logic of the application is defined as a class handling particular events. A Container instance is created and passed an instance of that handler class.The call torun()gives control to the Container, which performs the necessary IO operations and informs the handler of the events.Therun()returns when there is nothing to do.
3.4.2. Python Client Tutorials with examples
A Simple Sending and Receiving Program in Python
at-least-once guarantee, since each message should eventually be received at least once, though a given message may be received more than once.
				sent and total, where, sent keeps track of the number of messages that are send and total maintains a count of number of messages to send.
					Example 3.3. Sending reliable messages
- 1
- On_start()method is called when the Container first starts to run.In this example it is used to establish a sending link over which the messages are transferred.
- 2
- On_sendable()method is called to known when the messages can be transferred.The callback checks that the sender has credit before sending messages and if the sender has already sent the required number of messages.NoteAMQP provides flow control allowing any component receiving messages to avoid being overwhelmed by the number of messages it is sent. In this example messages are sent when the broker has enabled their flow.
- 3
- Send()is an asynchronous call. The return of the call does not indicate that the messages has been transferred yet.
- 4
- on_accepted()notifies if the amq broker has received and accepted the message.In this example, we use this event to track the confirmation of the messages sent. The connection closes and exits when the amq broker has received all the messages.NoteThe- on_accepted()call will be made by the Container when the amq broker accepts the message and not the receiving client.
- 5
- Resets the sent count to reflect the confirmed messages. The library automatically reconnects to the sender and hence when the sender is ready, it can restart sending the remaining messages
Example 3.4. Receiving reliable messages
examples queue on a broker accessible on port 5672 on localhost. The program simply prints the body of the received messages.
					- 1
- On_start()method is called when the Container first starts to run.In this example it is used to establish a receiving link over which the messages are transferred.
- 2
- On_message()method is called when a message is received. It simply prints the messagesIn this example, the amq broker waits for a certain number of messages before closing and exiting the connection. The method checks for duplicate messages and ignores them. The logic to ignore duplicates is implement using the sequential id scheme
Sending and Receiving Program using SSL in Python
SSL Configuration
SSL settings for A-MQ to run Qpid Python client
- Generate pem trust certificate for Qpid Python clientkeytool -importkeystore -srckeystore broker.ks -srcalias broker \ -srcstoretype JKS -deststoretype PKCS12 -destkeystore broker.pkcs12 \ -srcstorepass ${general_passwd} -deststorepass ${general_passwd} openssl pkcs12 -in broker.pkcs12 -out broker_cert.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}keytool -importkeystore -srckeystore broker.ks -srcalias broker \ -srcstoretype JKS -deststoretype PKCS12 -destkeystore broker.pkcs12 \ -srcstorepass ${general_passwd} -deststorepass ${general_passwd} openssl pkcs12 -in broker.pkcs12 -out broker_cert.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Adjust A-MQ broker to use the certificate by modifying the A-MQ environmentsed -i '/KARAF_OPTS/d' ${A_MQ_HOME}/bin/setenvsed -i '/KARAF_OPTS/d' ${A_MQ_HOME}/bin/setenvCopy to Clipboard Copied! Toggle word wrap Toggle overflow where,A_MQ_HOMEis the installation path of the amq broker executable file.echo "export KARAF_OPTS=\"-Djavax.net.ssl.keyStore=${certificates_dir}/broker.ks \ -Djavax.net.ssl.keyStorePassword=${general_passwd}\"" >> ${A_MQ_HOME}/bin/setenvecho "export KARAF_OPTS=\"-Djavax.net.ssl.keyStore=${certificates_dir}/broker.ks \ -Djavax.net.ssl.keyStorePassword=${general_passwd}\"" >> ${A_MQ_HOME}/bin/setenvCopy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Generate the client certificatekeytool -genkey -alias client -keyalg RSA -keystore client.ks \ -storepass ${general_passwd} -keypass ${general_passwd} \ -dname "O=Client,CN=client" -validity 99999keytool -genkey -alias client -keyalg RSA -keystore client.ks \ -storepass ${general_passwd} -keypass ${general_passwd} \ -dname "O=Client,CN=client" -validity 99999Copy to Clipboard Copied! Toggle word wrap Toggle overflow keytool -export -alias client -keystore client.ks -file client_cert \ -storepass ${general_passwd}keytool -export -alias client -keystore client.ks -file client_cert \ -storepass ${general_passwd}Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Add client certificate as trusted to the broker databasekeytool -import -alias client -keystore broker.ts -file client_cert \ -storepass ${general_passwd} -v -trustcacerts -nopromptkeytool -import -alias client -keystore broker.ts -file client_cert \ -storepass ${general_passwd} -v -trustcacerts -nopromptCopy to Clipboard Copied! Toggle word wrap Toggle overflow 
SSL certificate and keys settings for Qpid Python client
- Set SLL to prevent the private key and the certificate to be send to output.openssl pkcs12 -nocerts -in client.pkcs12 -out client_private_key.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}openssl pkcs12 -nocerts -in client.pkcs12 -out client_private_key.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}Copy to Clipboard Copied! Toggle word wrap Toggle overflow openssl pkcs12 -nokeys -in client.pkcs12 -out client_cert.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}openssl pkcs12 -nokeys -in client.pkcs12 -out client_cert.pem \ -passin pass:${general_passwd} -passout pass:${general_passwd}Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Adjust A-MQ broker to use the certificatesed -i '/KARAF_OPTS/d' ${A_MQ_HOME}/bin/setenvsed -i '/KARAF_OPTS/d' ${A_MQ_HOME}/bin/setenvCopy to Clipboard Copied! Toggle word wrap Toggle overflow echo "export KARAF_OPTS=\"-Djavax.net.ssl.keyStore=${certificates_dir}/broker.ks \ -Djavax.net.ssl.keyStorePassword=${general_passwd} \ -Djavax.net.ssl.trustStore=${certificates_dir}/broker.ts \ -Djavax.net.ssl.trustStorePassword=${general_passwd}\"" >> ${A_MQ_HOME}/bin/setenvecho "export KARAF_OPTS=\"-Djavax.net.ssl.keyStore=${certificates_dir}/broker.ks \ -Djavax.net.ssl.keyStorePassword=${general_passwd} \ -Djavax.net.ssl.trustStore=${certificates_dir}/broker.ts \ -Djavax.net.ssl.trustStorePassword=${general_passwd}\"" >> ${A_MQ_HOME}/bin/setenvCopy to Clipboard Copied! Toggle word wrap Toggle overflow 
Example
Example 3.5. Sending reliable messages over a secured connection
- 1
- set_trusted_ca_db("/path/to/ca-certificate.pem")call specifies the location of the CA's certificate in pem file format- set_peer_authentication(SSLDomain.VERIFY_PEER)call requests the servers certificate to be verified as valid using the specified CA's public key.To verify if the hostname used matches which the name specified in the servers certificate, replace the- VERIFY_PEERmacro to- VERIFY_PEER_NAME.NoteEnsure to update the program with the path of the certificates as per your environment before running the example.
- 2
- set_credentials("/path/to/client-certificate.pem", "/path/to/client-private-key.pem", "client-password")call is used if the client needs to authenticate itself. In such a case, you need to mention the clients public certificate, private key file both in pem format, and also specify the password required for the private keyNoteEnsure to update the program with the path of the client certificate, client private key as per your environment and the correct client-password before running the example.
A Request/Response Server and Client Program
Example 3.6. A simple server program to send responses
- 1
- On_message()method performs a lookup at the- reply_toaddress on the- messageand creates a sender over which the response can be send.In case there are more requests with the same- reply_toaddress, the method will store the senders.
Run python client over SSL
- No server and client authentication./sender.py -b "amqps://$(hostname):5672/examples" ./sender.py -b "amqps://$(hostname):5672/examples"Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Server authentication enabled./sender.py -b "amqps://$(hostname):5672/examples" --conn-ssl-trust-store<certificates_dir>/broker_cert.pem --conn-ssl-verify-peer --conn-ssl-verify-peer-name ./sender.py -b "amqps://$(hostname):5672/examples" --conn-ssl-trust-store<certificates_dir>/broker_cert.pem --conn-ssl-verify-peer --conn-ssl-verify-peer-nameCopy to Clipboard Copied! Toggle word wrap Toggle overflow 
- Server and client authentication enabled./sender.py -b "amqps://$(hostname):5672/examples" --conn-ssl-certificate <certificates_dir>/client-certificate.pem --conn-ssl-private-key <certificates_dir>/client-private-key.pem --conn-ssl-trust-store <certificates_dir>/broker_cert.pem --conn-ssl-verify-peer --conn-ssl-verify-peer-name ./sender.py -b "amqps://$(hostname):5672/examples" --conn-ssl-certificate <certificates_dir>/client-certificate.pem --conn-ssl-private-key <certificates_dir>/client-private-key.pem --conn-ssl-trust-store <certificates_dir>/broker_cert.pem --conn-ssl-verify-peer --conn-ssl-verify-peer-nameCopy to Clipboard Copied! Toggle word wrap Toggle overflow 
A simple client program to receive the messages from the Server
examples broker.
				Example 3.7. A simple client program to receive the messages from the Server
- 1
- On_start()method creates a receiver to receive the responses from the serverIn this example, instead of using the localhost, we set the dynamic option which informs the amq broker that the client is connected to create a temporary address over which it can receive the responses.
- 2
- On_link_opened()method sends the first requests if the receiving link is setup and confirmed by the brokerHere, we use the address allocated by the broker as the reply_to address of the requests hence the broker needs to confirm that the receiving link is established.
Sending and Receiving using Transactions
TransactionHandler in addition to MessagingHandler as a base class for the handler definition.
				Example 3.8. Sending messages using local transactions
- 1
- on_transaction_declared()method requests a new- transactionalcontext, passing themselves as the handler for the transaction
- 2
- on_transaction_declared()method is notified when that context is in place
- 5
- When theon_transaction_committed()method is called the committed count is incremented by the size of the current batch. If the committed count after that is equal to the number of message it was asked to send, it has completed all its work so closes the connection and the program will exit. If not, it starts a new transaction and sets the current batch to 0.The sender tracks the number of messages sent in the current_batch, as well as the number of messages committed .
- 3
- Messages are sent when the transaction context has been declared and there is credit. Thesend()method of the transaction is invoked, rather than on the sender itself. This ensures that send operation is tied to that transaction context.
- 4
- Thecurrent_batchcounter is incremented for each message. When that counter reaches the preconfigured batch size, thecommit()method is called on the transaction.
Example
current_batch and the overall number committed
				Example 3.9. Receiving using local transactions
- 1
- on_message()method the receiver calls the- accept()method on the transaction object to tie the acceptance to the context. It then increments the- current_batch. If the- current_batchis now equal to the- batch_size, the receiver calls the- commit()method on the transaction.
- 2
- on_transaction_declared()method controls the message flow. The receiver uses the- flow()method on the receiver to request an equal number of messages that match the- batch_size
- 3
- When theon_transaction_committed()method is called the committed count is incremented, the application then tests whether it has received all expected messages. If all the messages are received, the application exists. If all messages are not received a new transactional context is declared and the batch is reset.
Using a Selector Filter
Example 3.10. Filtering messages using a selector
- 1
- on_start()method implements a selector that filters messages based on the message headerWhile creating the receiver, specify the Selector object as an option. The options argument can be a single object or a list.
- Literals- A string literal must be enclosed in single quotes with an included single quote represented by doubled single quote such as ‘literal’ and ‘literal’’s’
- Numeric literals, both whole numbers and those with decimal points.
- Boolean literals TRUE and FALSE.
 
- Expressions- Expressions can contain logical expressions such as NOT, AND, OR and comparison operators =, >, >=, <, <=, <> (not equal)
- They can also contain arithmetic operators +, -, /, *,
- Pattern values can also be used. These are string literals where_stands for any single character and%stands for any sequence of characters. You can escape the special meaning of_and%by using the escape character. For example:number LIKE ‘12%3’ is true for ‘123’ ‘12993’ and false for ‘1234’ word LIKE ‘f_ll’ is true for ‘fall’ and 'full' but not for ‘fail’ variable LIKE ‘_%’ ESCAPE ‘_’ is true for ‘_thing’ and false for ‘thing’ number LIKE ‘12%3’ is true for ‘123’ ‘12993’ and false for ‘1234’ word LIKE ‘f_ll’ is true for ‘fall’ and 'full' but not for ‘fail’ variable LIKE ‘_%’ ESCAPE ‘_’ is true for ‘_thing’ and false for ‘thing’Copy to Clipboard Copied! Toggle word wrap Toggle overflow 
 
Sending and Receiving Best-Effort Messages
AtMostOnce to the options keyword arguments to Container.create_sender and Container.create_receiver. For AtMostOnce, the sender settles the message as soon as it sends it. If the connection is lost before the message is received by the receiver, the message will not be delivered. The AtMostOnce link option type is defined in proton.reactors.
				Example 3.11. Receiving best-effort messages
- 1
- on_start()method uses the AtMostOnce option to receive the unacknowledged messages.If the connection is lost before the message is received by the receiver, the message will not be delivered.
on_accepted method is redundant. There is no distinction between confirmed and sent status and the on_disconnected method is redundant. Any shutdown would be triggered directly after sending.
				Example 3.12. Sending best-effort messages
- 1
- on_start()method uses the AtMostOnce option to send the unacknowledged messages.