Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: communicate small messages between machines with Socket Datagrams

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Accept UDP datagrams, on the given port, from any sender
s.bind(("", 8081))
pr

Python: Read POP3 E-mail messages

import poplib
import getpass
serveur = poplib.POP3('mail.example.com')
#User Authentication
serveur.user(getpass.getuser())
serveur.pass_(getpass.g

Python: How to read email message from .msg file

import email
def read_MSG(file):
    email_File = open(file)
    messagedic = email.Message(email_File)
    content_type = messagedic["plain/text"]

Python: How to Error Handling Example

import socket
# create Internet Socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEA

Python: Find out if by default sockets are allowed to reuse addresses

import socket
s = socket.socket( )
print s.getsockopt(s.SOL_SOCKET, s.SO_REUSEADDR)

Python: How to Get local Hostname

import socket
hostname = socket.gethostname()
print "Host name:", hostname

Python: How to Extract list of URLs in a web page

"""
Tiré du livre gratuit "Plongez au coeur de Python",
Pour plus d'information.  Visitez http://diveintopython.net/
"""
__author__ = "Mark Pi

Python: How to download from a website

import urllib
import sys
f = urllib.urlopen("http://www.google.com")
while 1:
    buf = f.read(2048)
    if not len(buf):
        break
    sys.std

Python:Using the urllib Module to Fetch a Remote Resource

import urllib, sys
f = urllib.urlopen("http://www.yahoo.com")
while 1:
    buf = f.read(2048)
    if not len(buf):
        break
    sys.stdout.wri

Python: send file to ftp server

import ftplib
def send_File(file, user, pwd):
    ftp = ftplib.FTP("ftp.example.com")
    ftp.login(user, pwd)
    ftp.storlines("STOR " + file, op

Python: How to use urllib2 with basic HTTP authentication

import urllib2
prog_URL = urllib2.HTTPPasswordMgrWithDefaultRealm( )
prog_URL.add_password(None, 'http://premium.tubedir.com/', 'username',
    'pa

Python: reverse DNS lookup program

import sys, socket
try:
    result = socket.gethostbyaddr("209.85.148.138")
    print "Hostname:"
    print "  " + result[0]
except socket.herror,

Python: How to transfer files between FTP servers

import ftplib
from_ftp = ftplib.FTP("www.site1.com", "login", "password")
from_ftp.cwd("/htdoc")
to_ftp = ftplib.FTP("www.site2.com", "login", "pa

Python: reverse DNS lookup program

import sys, socket
try:
    result = socket.gethostbyaddr("209.85.148.138")
    print "Hostname:"
    print "  " + result[0]
except socket.herror,

Python: Attach a file to an email

import os
from email import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_MIME(FROM