Python: "subject" not shown when sending email using smtplib module. L’encoding en Python, une bonne fois pour toute. J’avais oublié la zik, je rajoute: Vous avez tous un jour eu l’erreur suivante : UnicodeDecodeError: 'machine' codec can't decode character 'trucmuche' in position x: ordinal not in range(z) Et là, pour vous en sortir, vous en avez chié des ronds de pâté. Le problème vient du fait que la plupart du temps, ignorer l’encoding marche : nous travaillons dans des environnements homogènes et toujours avec des données dans le même format, ou un format plus ou moins compatible. Mais le texte, c’est compliqué, terriblement compliqué, et le jour où ça se gâte, si vous ne savez pas ce que vous faites, vous ne vous en sortirez pas. C’est d’autant plus vrai en Python car : Par défaut, Python plante sur les erreurs d’encoding là où d’autres langages (comme le PHP) se débrouillent pour vous sortir un truc (qui ne veut rien dire, qui peut corrompre toute votre base de données, mais qui ne plante pas).Python est utilisé dans des environnements hétérogènes.
Règle numéro 1 : Le texte brut n’existe pas. Python Regular Expressions. A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. Regular expressions are widely used in UNIX world. The module re provides full support for Perl-like regular expressions in Python. The re module raises the exception re.error if an error occurs while compiling or using a regular expression. We would cover two important functions, which would be used to handle regular expressions. But a small thing first: There are various characters, which would have special meaning when they are used in regular expression. To avoid any confusion while dealing with regular expressions, we would use Raw Strings as r'expression'. The match Function This function attempts to match RE pattern to string with optional flags. Here is the syntax for this function − re.match(pattern, string, flags=0) Here is the description of the parameters: Example #!
The search Function re.search(pattern, string, flags=0) Python Networking Programming. Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols. Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. This chapter gives you understanding on most famous concept in Networking - Socket Programming. What is Sockets? Sockets are the endpoints of a bidirectional communications channel.
Sockets may communicate within a process, between processes on the same machine, or between processes on different continents. Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. Sockets have their own vocabulary: The socket Module To create a socket, you must use the socket.socket() function available in socket module, which has the general syntax − A Simple Server #!
Python Networking Programming. Python Object Oriented. Python has been an object-oriented language since it existed. Because of this, creating and using classes and objects are downright easy. This chapter helps you become an expert in using Python's object-oriented programming support. If you do not have any previous experience with object-oriented (OO) programming, you may want to consult an introductory course on it or at least a tutorial of some sort so that you have a grasp of the basic concepts. However, here is small introduction of Object-Oriented Programming (OOP) to bring you at speed − Overview of OOP Terminology Class: A user-defined prototype for an object that defines a set of attributes that characterize any object of the class.
The attributes are data members (class variables and instance variables) and methods, accessed via dot notation. Creating Classes The class statement creates a new class definition. Class ClassName: 'Optional class documentation string' class_suite Example Following is the example of a simple Python class − Python Sending Email using SMTP. Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail servers.
Python provides smtplib module, which defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon. Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail − import smtplib smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] ) Here is the detail of the parameters: host: This is the host running your SMTP server.
An SMTP object has an instance method called sendmail, which is typically used to do the work of mailing a message. The sender - A string with the address of the sender.The receivers - A list of strings, one for each recipient.The message - A message as a string formatted as specified in the various RFCs. Example Here is a simple way to send one e-mail using Python script. . #! Smtplib.SMTP('mail.your-domain.com', 25) Sending an HTML e-mail using Python #! #! Unicode encoding and decoding — Plone Documentation v4.3. Introduction: Why unicode is difficult? Python 2.x does not make a clear distinction between: 8-bit strings (byte data)16-bit unicode strings (character data) Developers use these two formats interchangeably, because it is so easy and Python does not warn you about this.
However, it will only work as long as the input does not encounter any international, non-ASCII, characters. When 8-bit encoded string data and 16-bit raw Unicode string data gets mixed up, by being run through encoding first, really nasty things start to happen. Read more: safe_unicode() Plone's core contains a helper function which allows you to safely decode strings to unicode without fear of UnicodeDecodeException. Example: # -*- coding: utf-8 -*- from Products.CMFPlone.utils import safe_unicode foobar = safe_unicode("Ärrinmurrin taas on Plonea joku jättänyt dokumentoimatta") UnicodeEncodeError do: Step 011 - Keys, Rotating and Zoom [The Python Game Book]
Code Discussion Pressed Keys Surfaces can not only moved around, but also rotated and -unlike beer mats- zoomed. The next source code examples introduce a new method of keyboard control. Instead of checking a queued event with pygame.event.get() the function pygame.key.get_pressed() delivers the actual state of the complete keyboard. Pressedkeys = pygame.key.get_pressed()if pressedkeys[pygame.K_x]: do_something() checks if the key “x” is pressed. In the source code example bleow, the cursor keys ← ↑ → ↓ are used to move the snake surface, while the keys W and S zoom/shrink the snake and the keys A and D rotate the snake. It is possible to press several keys together, like left and right cursor, and the program will move the correctly (not at all in this case): dx, dy = 0, 0 # no cursor key, no movement if pressedkeys[pygame.K_LEFT]: dx -= speed if pressedkeys[pygame.K_RIGHT]: dx += speed troublesome subsurface do not get lost in space !
Zooming around the Center snake_original = snake #! Log watcher (tail -F *.log) « Python recipes « ActiveState Code. Basic usage same as: tail -F /var/log/*.log def callback(filename, lines): for line in lines: print(line) watcher = LogWatcher("/var/log/", callback)watcher.loop() Also read last N lines from files before start watching same as: tail -F /var/log/*.log -n 20 watcher = LogWatcher("/var/log/", callback, tail_lines=20)watcher.loop() Tail last N lines from a single file only same as: tail -n 10 foo.log LogWatcher.tail('foo.log', 10) Non blocking import time watcher = LogWatcher("/var/log/", callback)while 1: print("loop") watcher.loop(blocking=False) time.sleep(0.1) Coloured logs In case your python application is using the logging module you might want to monitor what it's doing in real time and have a coloured ouput.
Import logging logging.basicConfig(level=logging.DEBUG, format='[%(levelname)1.1s %(asctime)s] %(message)s',) ...you'll have log lines looking like this: [I 2011-11-29 19:26:44,774] info message[D 2011-11-29 19:26:44,774] debug message[E 2011-11-29 19:26:44,774] some error message. Audio - Play a Sound with Python. For Beginners.
Welcome! Are you completely new to programming? If not then we presume you will be looking for information about why and how to get started with Python. Fortunately an experienced programmer in any programming language (whatever it may be) can pick up Python very quickly. It's also easy for beginners to use and learn, so jump in! Installing Python is generally easy, and nowadays many Linux and UNIX distributions include a recent Python.
Even some Windows computers (notably those from HP) now come with Python already installed. If you do need to install Python and aren't confident about the task you can find a few notes on the BeginnersGuide/Download wiki page, but installation is unremarkable on most platforms. If you want to know whether a particular application, or a library with particular functionality, is available in Python there are a number of possible sources of information. If you want to help to develop Python, take a look at the developer area for further information. What python library to use for non-blocking audio I/O on OSX?