<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>PasteQuestion.com</title>
	
	<link>http://www.pastequestion.com/blog</link>
	<description />
	<lastBuildDate>Fri, 17 Jul 2009 08:39:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/PasteQuestion_BLOG" /><feedburner:info uri="pastequestion_blog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>How to script Realtime backups in Python</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/LMzg_W3xUN8/how-to-script-realtime-backups-in-python.html</link>
		<comments>http://www.pastequestion.com/blog/python/how-to-script-realtime-backups-in-python.html#comments</comments>
		<pubDate>Tue, 14 Jul 2009 16:49:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=125</guid>
		<description><![CDATA[In this tutorial we shall show you how easy it is to make simple real-time backup software by using the Python module called Pyinotify. This module is based on the Linux kernel feature named inotify (included since kernel 2.6.13); and this being an event-driven notifier, it'll convey its notifications from kernel space to user space by means of using system-calls. The Pyinotify module will eventually bind such calls.]]></description>
			<content:encoded><![CDATA[<p>In this tutorial we shall show you how easy it is to make simple real-time backup software by using the Python module called Pyinotify. This module is based on the Linux kernel feature named inotify (included since kernel 2.6.13); and this being an event-driven notifier, it&#8217;ll convey its notifications from kernel space to user space by means of using system-calls. The Pyinotify module will eventually bind such calls.</p>
<p>Let&#8217;s get to the code now: create a file labelled &#8220;backup.py&#8221; in your working directory and paste in the following code:</p>
<p></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>, <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">signal</span>, <span style="color: #dc143c;">sys</span>, <span style="color: #dc143c;">operator</span>, <span style="color: #dc143c;">glob</span>, <span style="color: #dc143c;">shutil</span>, <span style="color: #dc143c;">filecmp</span>
<span style="color: #ff7700;font-weight:bold;">from</span> pyinotify <span style="color: #ff7700;font-weight:bold;">import</span> WatchManager, Notifier, ThreadedNotifier, EventsCodes, ProcessEvent
&nbsp;
&nbsp;
BACKUP = <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">&quot;/home/user/my_dir&quot;</span> : <span style="color: #483d8b;">&quot;/home/user/my_backup&quot;</span>,
        <span style="color: #483d8b;">&quot;/home/user/test&quot;</span> : <span style="color: #483d8b;">&quot;/home/user/test_backup&quot;</span>        
<span style="color: black;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> FileSystemEvent<span style="color: black;">&#40;</span>ProcessEvent<span style="color: black;">&#41;</span>:
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, watcher<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">f</span> = FileBackup<span style="color: black;">&#40;</span>watcher<span style="color: black;">&#41;</span>                
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> process_event<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">f</span>.<span style="color: black;">process_file</span><span style="color: black;">&#40;</span>event.<span style="color: black;">path</span>, event.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> process_IN_CREATE<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">process_event</span><span style="color: black;">&#40;</span>event<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> process_IN_DELETE<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">process_event</span><span style="color: black;">&#40;</span>event<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> process_IN_MODIFY<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">process_event</span><span style="color: black;">&#40;</span>event<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> FileBackup:
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, watcher<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">watcher</span> = watcher
                <span style="color: #008000;">self</span>.<span style="color: black;">l</span> = BACKUP.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> process_file<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event_path, event_name<span style="color: black;">&#41;</span>:
                path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>event_path, event_name<span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
                        <span style="color: #008000;">self</span>.<span style="color: black;">watcher</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#watch new dir</span>
                        base = <span style="color: #008000;">self</span>.<span style="color: black;">get_base_path</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                        base = <span style="color: #008000;">self</span>.<span style="color: black;">get_base_path</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">dirname</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
                dst = BACKUP<span style="color: black;">&#91;</span>base<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>+base<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                        <span style="color: #dc143c;">os</span>.<span style="color: black;">makedirs</span><span style="color: black;">&#40;</span>dst<span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isfile</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
                        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isfile</span><span style="color: black;">&#40;</span>dst+<span style="color: #483d8b;">&quot;/&quot;</span>+event_name<span style="color: black;">&#41;</span>:
                                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">filecmp</span>.<span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>path, dst+<span style="color: #483d8b;">&quot;/&quot;</span>+event_name<span style="color: black;">&#41;</span>:
                                        <span style="color: #ff7700;font-weight:bold;">return</span>                                
&nbsp;
                        <span style="color: #dc143c;">shutil</span>.<span style="color: black;">copy2</span><span style="color: black;">&#40;</span>path, dst+<span style="color: #483d8b;">&quot;/&quot;</span>+event_name<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> get_base_path<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, path<span style="color: black;">&#41;</span>:                
                <span style="color: #ff7700;font-weight:bold;">for</span> p <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">l</span>:
                        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>path.<span style="color: black;">replace</span><span style="color: black;">&#40;</span>p, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
                                <span style="color: #ff7700;font-weight:bold;">return</span> p, path.<span style="color: black;">replace</span><span style="color: black;">&#40;</span>p, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;</span>,<span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> FileSystemWatcher<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, dirs=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">wm</span>       = WatchManager<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">fse</span>      = FileSystemEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #008000;">self</span>.<span style="color: black;">mask</span>     = EventsCodes.<span style="color: black;">IN_DELETE</span> | EventsCodes.<span style="color: black;">IN_CREATE</span> | EventsCodes.<span style="color: black;">IN_MODIFY</span> <span style="color: #808080; font-style: italic;"># events</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">notifier</span> = ThreadedNotifier<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">wm</span>, <span style="color: #008000;">self</span>.<span style="color: black;">fse</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">dir</span> <span style="color: #ff7700;font-weight:bold;">in</span> dirs: <span style="color: #008000;">self</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#41;</span>               
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">notifier</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">notifier</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> close<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">notifier</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> add<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, dirpath<span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>dirpath<span style="color: black;">&#41;</span>:
                        <span style="color: #008000;">self</span>.<span style="color: black;">wdd</span> = <span style="color: #008000;">self</span>.<span style="color: black;">wm</span>.<span style="color: black;">add_watch</span><span style="color: black;">&#40;</span>dirpath, <span style="color: #008000;">self</span>.<span style="color: black;">mask</span>, rec=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> KeyboardCatcher:
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">child</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">fork</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">child</span> == <span style="color: #ff4500;">0</span>:
                        <span style="color: #ff7700;font-weight:bold;">return</span>
                <span style="color: #ff7700;font-weight:bold;">else</span>:
                        <span style="color: #008000;">self</span>.<span style="color: black;">watch</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> watch<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                        <span style="color: #dc143c;">os</span>.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">KeyboardInterrupt</span>:
                        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'KeyBoardInterrupt'</span>
                        <span style="color: #008000;">self</span>.<span style="color: black;">kill</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #dc143c;">sys</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>                               
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> kill<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">try</span>:
                        <span style="color: #dc143c;">os</span>.<span style="color: black;">kill</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">child</span>, <span style="color: #dc143c;">signal</span>.<span style="color: black;">SIGKILL</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">OSError</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> _copy_base_dir<span style="color: black;">&#40;</span>src, dst<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #dc143c;">os</span>.<span style="color: black;">makedirs</span><span style="color: black;">&#40;</span>dst<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">OSError</span>:                
                <span style="color: #ff7700;font-weight:bold;">pass</span> <span style="color: #808080; font-style: italic;"># Directory already exists!</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">for</span> path <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #dc143c;">glob</span>.<span style="color: #dc143c;">glob</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%s/*&quot;</span> <span style="color: #66cc66;">%</span> src<span style="color: black;">&#41;</span>:
                dst2 = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>dst, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isdir</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
                        _copy_base_dir<span style="color: black;">&#40;</span>path, dst2<span style="color: black;">&#41;</span>                        
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isfile</span><span style="color: black;">&#40;</span>path<span style="color: black;">&#41;</span>:
                        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">isfile</span><span style="color: black;">&#40;</span>dst2<span style="color: black;">&#41;</span>:
                                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">filecmp</span>.<span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>path, dst2<span style="color: black;">&#41;</span>:
                                        <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
                        <span style="color: #dc143c;">shutil</span>.<span style="color: black;">copy2</span><span style="color: black;">&#40;</span>path, dst<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> copy_base_dir<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> src <span style="color: #ff7700;font-weight:bold;">in</span> BACKUP: 
                _copy_base_dir<span style="color: black;">&#40;</span>src, BACKUP<span style="color: black;">&#91;</span>src<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>        
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:         
        KeyboardCatcher<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#copy_base_dir()</span>
&nbsp;
        w = FileSystemWatcher<span style="color: black;">&#40;</span>BACKUP.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        w.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>          
        w.<span style="color: black;">block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">&quot;__main__&quot;</span>:
        main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>With this script we&#8217;ll be able to backup our files in real-time.<br />
First off we created a dictionary called BACKUP, in which we set those directories which are to be monitored and where we&#8217;re going to save all our files; in this way we can back-up one or more directories.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">BACKUP = <span style="color: black;">&#123;</span>
        <span style="color: #483d8b;">&quot;from1&quot;</span> : <span style="color: #483d8b;">&quot;to1&quot;</span>,
        <span style="color: #483d8b;">&quot;from2&quot;</span> : <span style="color: #483d8b;">&quot;to2&quot;</span>
<span style="color: black;">&#125;</span></pre></div></div>

<p>Now&#8230; on with the main() function:</p>
<p></p>
<p>The first instruction, <strong>KeyboardCatcher()</strong>, provides us with a new process in our program. This class is not mandatory, but allows us to catch any possible keyboard exceptions (ctrl-c). The main program will stand by, waiting; whilst the child process can continue following the script. </p>
<p>We commented out the call to the <strong>copy_base_dir()</strong> method, but you could use it as best suits your necessities. In a nutshell this method will back-up the directories—before starting with the file-system monitoring—and skip every already-existing file.</p>
<p>We reach now the program&#8217;s very core, that is monitoring the file-system using python.<br />
We&#8217;ve done a class called <strong>FileSystemWatcher</strong> to which we pass the list of files to be monitored. This class has the office of &#8216;listening&#8217; for file-system events via the Pyinotify module.</p>
<p>Of course, we&#8217;re the ones to appoint which events are to be captured. Indeed, with the following line, we choose the events we want secured.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">self</span>.<span style="color: black;">mask</span> = EventsCodes.<span style="color: black;">IN_DELETE</span> | EventsCodes.<span style="color: black;">IN_CREATE</span> | EventsCodes.<span style="color: black;">IN_MODIFY</span></pre></div></div>

<p>Establishing the events&#8217; names is not enough though, we&#8217;ve to make a class whose callback functions are triggered by the occurrence of such event; in this case it&#8217;s the class <strong>FileSystemEvent</strong>  with its three callbacks: <em>process_IN_CREATE &#8211; process_IN_DELETE – process_IN_MODIFY</em>.</p>
<p>Finally the class <strong>FileBackup</strong>, thanks to the process_file method, will check whether the file to be saved is already in the backup directory.</p>
<p>As previously addressed, this is a very simple script to make backups and could be surely improved and optimized.</p>
<p>Well, have fun!</p>
<p></p>
<p><em><strong>NOTE:</strong> The script has been tested with Python 2.5.2 and, needless to say, with GNU Linux OS; those of you who are MS-windows dependent should probably fast for this time <img src='http://www.pastequestion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </em></p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/LMzg_W3xUN8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/python/how-to-script-realtime-backups-in-python.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/python/how-to-script-realtime-backups-in-python.html</feedburner:origLink></item>
		<item>
		<title>Is your code fast? Find out with timeit!</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/AP8L1fShidU/is-your-code-fast-find-out-with-timeit.html</link>
		<comments>http://www.pastequestion.com/blog/python/is-your-code-fast-find-out-with-timeit.html#comments</comments>
		<pubDate>Wed, 08 Jul 2009 11:43:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=109</guid>
		<description><![CDATA[I bet many of you have wondered, at least once, how much time your method or class is going to take when executed. Well, thanks to the python timeit module we can really time our own code.
Quite often when tweaking with the code, it would be quite a good practice to test such tweaks by means of  timing them; in this way we could be able to see which tweaked version of the code takes the more time. In this tutorial we're going into the timeit module which has been added since python 2.3.]]></description>
			<content:encoded><![CDATA[<p>I bet many of you have wondered, at least once, how much time your method or class is going to take when executed. Well, thanks to the python timeit module we can really time our own code.<br />
Quite often when tweaking with the code, it would be quite a good practice to test such tweaks by means of  timing them; in this way we could be able to see which tweaked version of the code takes the more time. In this tutorial we&#8217;re going into the timeit module which has been added since python 2.3.</p>
<p>Using the aforementioned module is definitely simple; indeed with just three lines of code we shall be able to assess the execution time of our script, or part of it that is.</p>
<p></p>
<p>Let&#8217;s create a file called &#8220;test_timeit.py&#8221; in our working directory and let&#8217;s also copy the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">timeit</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Tester:
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">MAX</span> = <span style="color: #ff4500;">100</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">my_list</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> add_to_list<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">MAX</span><span style="color: black;">&#41;</span>:
                        <span style="color: #008000;">self</span>.<span style="color: black;">my_list</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span>i<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> remove_from_list<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> elem <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">my_list</span>:
                        <span style="color: #008000;">self</span>.<span style="color: black;">my_list</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span>elem<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
        inst = Tester<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        t1   = <span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;inst.add_to_list()&quot;</span>, <span style="color: #483d8b;">&quot;from __main__ import inst&quot;</span><span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;">#t1  = timeit.Timer(&quot;inst.remove_from_list()&quot;, &quot;from __main__ import inst&quot;)</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
                <span style="color: #ff7700;font-weight:bold;">print</span> t1.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">200</span><span style="color: black;">&#41;</span>        
		<span style="color: #ff7700;font-weight:bold;">print</span> t1.<span style="color: black;">repeat</span><span style="color: black;">&#40;</span>repeat=<span style="color: #ff4500;">3</span>, number=<span style="color: #ff4500;">200</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
                t1.<span style="color: black;">print_exc</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Now for the explanatory part:</p>
<p></p>
<p>First of all we created a simple class named &#8220;Tester&#8221;, thus bundling two methods which we shall use to determine the execution time; they&#8217;re called <em>add_to_list()</em> and <em>remove_to_list()</em> and they&#8217;ll take care of adding or removing elements from our list, named as &#8220;my_list&#8221;.</p>
<p>Let&#8217;s get to the <strong>__main__</strong> part now where, thanks to <strong>inst = Tester()</strong>, we shall create a new instance of our Tester class—this instance will let us reach each method which has to be timed.<br />
Then from line 21 onwards, we tackle the timeit part.</p>
<p>The first one:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">t1 = <span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;inst.add_to_list()&quot;</span>, <span style="color: #483d8b;">&quot;from __main__ import inst&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>by which we create an instance of the Timer class that will let us measure the execution time of our python code—each parameter used in this example is referring respectively to the method we time, namely <strong>&#8220;inst.add_to_list()&#8221;</strong>, and to the mandatory import which is enabling us to get an instance called <strong>inst</strong>.</p>
<p></p>
<p><em><strong>NOTE:</strong> We would get the same result with: t1 = timeit.Timer(&#8221;Tester().add_to_list()&#8221;, &#8220;from __main__ import Tester&#8221;)</em></p>
<p></p>
<p>Now, getting to the real time-appraising part, the Timer class deploys two methods:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>number=<span style="color: #ff4500;">1000000</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This method returns the execution time of the main statement, which has been recalled as many times as parameter &#8220;number&#8221; dictates; in our example(line 25) we&#8217;ve used <strong>print t1.timeit(200)</strong>, that is to say, the <em>add_to_list()</em> method of Tester class will be called for 200 times, at the end of which the amount of time(float) spent on such office will be returned.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">repeat<span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>repeat=<span style="color: #ff4500;">3</span><span style="color: black;">&#91;</span>, number=<span style="color: #ff4500;">1000000</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The <em>repeat()</em> method works in the same way of <em>timeit()</em>, just with a trifling variation; quite useful.<br />
The number specified by &#8220;repeat&#8221; pinpoints how many times we take the measure.<br />
As it is in our example, <strong>print t1.repeat(repeat=3, number=200)</strong>, the method add_to_list() of  Tester class is being recalled 200 times, at the end of which a new measure ensues, and so on as many times as it is indicated in the number &#8220;repeat&#8221;. As we can easily guess, the <em>repeat()</em> method returns a list with every recorded amount of time. </p>
<p></p>
<p>Here come other two simple and explanatory examples:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#Example 1</span>
<span style="color: #ff7700;font-weight:bold;">def</span> factorial<span style="color: black;">&#40;</span>x<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">if</span> x == <span style="color: #ff4500;">0</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff4500;">1</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		<span style="color: #ff7700;font-weight:bold;">return</span> x <span style="color: #66cc66;">*</span> factorial<span style="color: black;">&#40;</span>x-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__==<span style="color: #483d8b;">&quot;__main__&quot;</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">timeit</span> <span style="color: #ff7700;font-weight:bold;">import</span> Timer
    t = Timer<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;factorial(5)&quot;</span>, <span style="color: #483d8b;">&quot;from __main__ import factorial&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> t.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">#Example 2</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">timeit</span>
&nbsp;
s = <span style="color: #483d8b;">&quot;&quot;&quot;
for i in range(10):
    if i % 2 == 0:
        pass
&quot;&quot;&quot;</span>
t= <span style="color: #dc143c;">timeit</span>.<span style="color: black;">Timer</span><span style="color: black;">&#40;</span>s<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> t.<span style="color: #dc143c;">timeit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>As you can clearly see, using timeit is that easy, with ever so few steps we were able to time our python code.<br />
Well? What are you waiting for? Get on with your testing! <img src='http://www.pastequestion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p></p>
<p></p>
<p><em>created by Damiano Porta and translated by Daniele Feuli</em></p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/AP8L1fShidU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/python/is-your-code-fast-find-out-with-timeit.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/python/is-your-code-fast-find-out-with-timeit.html</feedburner:origLink></item>
		<item>
		<title>Send email with attachments using Python</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/R7UIgF0iKS8/send-email-with-attachments-using-python.html</link>
		<comments>http://www.pastequestion.com/blog/python/send-email-with-attachments-using-python.html#comments</comments>
		<pubDate>Tue, 07 Jul 2009 08:57:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=88</guid>
		<description><![CDATA[Today we will look at a method of using the SMTP protocol via Python. Our goal is to create a class through which we can send emails with attachments.]]></description>
			<content:encoded><![CDATA[<p>Today we will look at a method of using the SMTP protocol via Python. Our goal is to create a class through which we can send emails with attachments.</p>
<p>First, a word about the SMTP protocol.</p>
<ul>
<li><strong>SMTP</strong> (Simple Mail Transfer Protocol) is the standard protocol for sending email via internet. By default, SMTP uses port number 25 and the transmission protocol is TCP. One of the first SMTP servers is sendmail, still widely in use, others exist, such as <a href="http://www.postfix.org/" target="_blank">Postfix</a>, <a href="http://www.exim.org/" target="_blank">Exim</a>.</li>
</ul>
<p>Ok, on to the code. The main way of sending emails via python is the smtplib.</p>
<p>Create a file called &#8220;smtp.py&#8221; in your working directory and paste the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">smtplib</span>, <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">time</span>, <span style="color: #dc143c;">atexit</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEMultipart</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEMultipart
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEBase</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEBase
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">MIMEText</span> <span style="color: #ff7700;font-weight:bold;">import</span> MIMEText
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span>.<span style="color: black;">Utils</span> <span style="color: #ff7700;font-weight:bold;">import</span> formatdate
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">email</span> <span style="color: #ff7700;font-weight:bold;">import</span> Encoders
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> ConnectionError<span style="color: black;">&#40;</span><span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> LoginError<span style="color: black;">&#40;</span><span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> DisconnectionError<span style="color: black;">&#40;</span><span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> EmailSendError<span style="color: black;">&#40;</span><span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Smtp:
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, host, <span style="color: #dc143c;">user</span>, password, port=<span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._host        = host
                <span style="color: #008000;">self</span>._port        = port                
                <span style="color: #008000;">self</span>._user        = <span style="color: #dc143c;">user</span>
                <span style="color: #008000;">self</span>._password    = password
&nbsp;
                <span style="color: #008000;">self</span>._message     = <span style="color: #008000;">None</span>
                <span style="color: #008000;">self</span>._subject     = <span style="color: #008000;">None</span>
                <span style="color: #008000;">self</span>._from_addr   = <span style="color: #008000;">None</span>
                <span style="color: #008000;">self</span>._rcpt_to     = <span style="color: #008000;">None</span>               
                <span style="color: #008000;">self</span>._server      = <span style="color: #008000;">None</span>         
                <span style="color: #008000;">self</span>._attachments = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
&nbsp;
                <span style="color: #dc143c;">atexit</span>.<span style="color: black;">register</span><span style="color: black;">&#40;</span>close<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;">#our close() method will be automatically executed upon normal interpreter termination</span>
&nbsp;
                <span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> connect<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._host, <span style="color: #008000;">self</span>._port, <span style="color: #008000;">self</span>._user, <span style="color: #008000;">self</span>._password<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">try</span>:    
                                <span style="color: #008000;">self</span>._server = <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTP</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._host, <span style="color: #008000;">self</span>._port<span style="color: black;">&#41;</span>
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span>, e:
                                <span style="color: #ff7700;font-weight:bold;">raise</span> ConnectionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Connection failed!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">try</span>:
                                <span style="color: #008000;">self</span>._server.<span style="color: black;">login</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._user, <span style="color: #008000;">self</span>._password<span style="color: black;">&#41;</span>        
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span>, e:                                
                                <span style="color: #ff7700;font-weight:bold;">raise</span> LoginError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Login Failed!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> close<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:                
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>._server:
                        <span style="color: #ff7700;font-weight:bold;">try</span>:
                                <span style="color: #008000;">self</span>._server.<span style="color: black;">quit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span>, e:                                
                                <span style="color: #ff7700;font-weight:bold;">raise</span> DisconnectionError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Disconnection failed!&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> message<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._message = message
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> subject<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, subject<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._subject = subject
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> from_addr<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">email</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._from_addr = <span style="color: #dc143c;">email</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> rcpt_to<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">email</span><span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._rcpt_to = <span style="color: #dc143c;">email</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> attach<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>:
                        <span style="color: #008000;">self</span>._attachments.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> load_attachments<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, m_message<span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #008000;">file</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>._attachments:
                        part = MIMEBase<span style="color: black;">&#40;</span><span style="color: #483d8b;">'application'</span>, <span style="color: #483d8b;">&quot;octet-stream&quot;</span><span style="color: black;">&#41;</span>
                        part.<span style="color: black;">set_payload</span><span style="color: black;">&#40;</span><span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span>,<span style="color: #483d8b;">&quot;rb&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                        Encoders.<span style="color: black;">encode_base64</span><span style="color: black;">&#40;</span>part<span style="color: black;">&#41;</span>
                        part.<span style="color: black;">add_header</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Content-Disposition'</span>, <span style="color: #483d8b;">'attachment; filename=&quot;%s&quot;'</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span><span style="color: #008000;">file</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
                        m_message.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>part<span style="color: black;">&#41;</span>    
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">return</span> m_message
&nbsp;
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">def</span> send<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, content_type=<span style="color: #483d8b;">'plain'</span>, charset=<span style="color: #483d8b;">'UTF-8'</span><span style="color: black;">&#41;</span>:
&nbsp;
                <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #008000;">self</span>._message, <span style="color: #008000;">self</span>._subject, <span style="color: #008000;">self</span>._from_addr, <span style="color: #008000;">self</span>._rcpt_to<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:                                
&nbsp;
                        m_message             = MIMEMultipart<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
                        m_message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'From'</span><span style="color: black;">&#93;</span>     = <span style="color: #008000;">self</span>._from_addr
                        m_message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'To'</span><span style="color: black;">&#93;</span>       = <span style="color: #008000;">self</span>._rcpt_to
                        m_message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Date'</span><span style="color: black;">&#93;</span>     = formatdate<span style="color: black;">&#40;</span>localtime=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
                        m_message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'Subject'</span><span style="color: black;">&#93;</span>  = <span style="color: #008000;">self</span>._subject
                        m_message<span style="color: black;">&#91;</span><span style="color: #483d8b;">'X-Mailer'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">&quot;Python X-Mailer&quot;</span>
&nbsp;
                        m_message.<span style="color: black;">attach</span><span style="color: black;">&#40;</span>MIMEText<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._message, content_type, charset<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
                        m_message = <span style="color: #008000;">self</span>.<span style="color: black;">load_attachments</span><span style="color: black;">&#40;</span>m_message<span style="color: black;">&#41;</span>
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">try</span>:
                                <span style="color: #008000;">self</span>._server.<span style="color: black;">sendmail</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._from_addr, <span style="color: #008000;">self</span>._rcpt_to, m_message.<span style="color: black;">as_string</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>       
&nbsp;
                        <span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #dc143c;">smtplib</span>.<span style="color: black;">SMTPException</span>, e:
                                <span style="color: #ff7700;font-weight:bold;">raise</span> EmailSendError<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Email has not been sent&quot;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Here we have a simple class ready for sending email over an authenticated connection via python.</p>
<p>Now for the explanation (This should be easy to understand for those of us who already use python a bit).</p>
<p>Skipping over the various module import commands required to run our class, we come to the declaration of 4 classes (from line 8 to 11) needed for error management.</p>
<ul>
<li><strong>ConnectionError</strong> &#8211; the exception generated if there is a problem in connecting to the SMTP server.</li>
<li><strong>Loginerror</strong> &#8211; the exception generated when there is a problem with the login (user/password).</li>
<li><strong>DisconnectionError</strong> &#8211; the exception generated when there is a problem when disconnecting from SMTP server.</li>
<li><strong>EmailSendError</strong> &#8211; the exception generated when there is a problem in sending the email.</li>
</ul>
<p>As you can see every exception inherits smtplib.SMTPException. In this way you can also catch our exceptions, by using the baseclass smtplib module.</p>
<p>Now we move on to the explanation of <strong>SMTP class</strong>&#8230;</p>
<p>The class has an initialiser for the declaration of all attributes for the class and automatically calls the connect() method to connect to the SMTP server.</p>
<p>Moving on&#8230; at line number 33, we meet our connect() method, in which we will create an instance of the SMTP class (smtplib) and will then try to authenticate to the smtp server with self._server.login(). If there are errors in the connection or the login to the server, the two exceptions (ConnectionError, Loginerror) will be generated.</p>
<p>To successfully send an email certain data are needed: the sender, recipient, subject and message. For this reason, using our methods: from_addr(), rcpt_to(), subject(), and message() we will be able to upload all data needed to send our email. In addition to these basic methods, our class has a very useful method for attaching a file or files to our email, attach(file_path).</p>
<p>Now lets give it a try! Create a file named &#8220;test.py&#8221; in your working directory and paste the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> smtp <span style="color: #ff7700;font-weight:bold;">import</span> Smtp
&nbsp;
smtp = Smtp<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mail.server.com&quot;</span>, <span style="color: #483d8b;">&quot;user&quot;</span>, <span style="color: #483d8b;">&quot;password&quot;</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">subject</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'This is a test'</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">message</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Email message'</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">from_addr</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'from@server.com'</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">rcpt_to</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'to@server.com'</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">attach</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;file1.jpg&quot;</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">attach</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;file2.gif&quot;</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">attach</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;file3.pdf&quot;</span><span style="color: black;">&#41;</span>
smtp.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Need more explanation?   <img src='http://www.pastequestion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   </p>
<p>Have fun using this!</p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/R7UIgF0iKS8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/python/send-email-with-attachments-using-python.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/python/send-email-with-attachments-using-python.html</feedburner:origLink></item>
		<item>
		<title>Generating PDF Files Using PHP</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/AFZ5QMrefao/generating-pdf-files-using-php.html</link>
		<comments>http://www.pastequestion.com/blog/php/generating-pdf-files-using-php.html#comments</comments>
		<pubDate>Mon, 06 Jul 2009 21:54:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=81</guid>
		<description><![CDATA[Creating PDF files with PHP is actually much less confusing than it may at first sound. One extension in PHP 4, named the PDFLib extension, enables you to dynamically create PDF files with the PHP scripts that you have.]]></description>
			<content:encoded><![CDATA[<p>Creating PDF files with PHP is actually much less confusing than it may at first sound. One extension in PHP 4, named the PDFLib extension, enables you to dynamically create PDF files with the PHP scripts that you have.</p>
<p>If you have Windows, you already have a pre-installed PDF library that came with your package, so all you need to do is uncomment, or delete the # sign, from the appropriate lines. Linux users can download this extension at the website, <a href="http://www.pdflib.com/" target="blank">http://www.pdflib.com/</a>, but no matter which type of system you are working with, you need to have this installed before you can start. You also need to have Adobe Acrobat PDF reader installed on your computer. If you do not have this, you can easily download it for free at <a href="http://www.adobe.com/" target="_blank">http://www.adobe.com/</a>.</p>
<p>To read existing PDF files with PHP, you can also install the XPDF package on the website, <a href="http://www.foolabs.com/xpdf/about.html" target="_blank">http://www.foolabs.com/xpdf/about.html</a>. This program includes &#8220;pdftotext&#8221;.</p>
<p>If you have these programs, you are ready to begin. First, you need to create a simple PDF file and save it to your computer.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create handle for new PDF document</span>
<span style="color: #000088;">$pdf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pdf_new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open a file</span>
<span style="color: #990000;">pdf_open_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;dogs.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// start a new page (A4)</span>
<span style="color: #990000;">pdf_begin_page</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">550</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">860</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set a fill colour</span>
<span style="color: #990000;">pdf_setcolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rgb&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set a stroke colour</span>
<span style="color: #990000;">pdf_setcolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;stroke&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rgb&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// draw a rectangle</span>
<span style="color: #990000;">pdf_rect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">45</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">480</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">210</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">320</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">pdf_fill_stroke</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set a fill colour</span>
<span style="color: #990000;">pdf_setcolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fill&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rgb&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set a stroke colour</span>
<span style="color: #990000;">pdf_setcolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;stroke&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rgb&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// get and use a font object</span>
<span style="color: #000088;">$arial</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pdf_findfont</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;verdana&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;host&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #990000;">pdf_setfont</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #000088;">$verdana</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">12</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print text</span>
<span style="color: #990000;">pdf_show_xy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Dogs have made excellent companions to humans for hundreds of years,&quot;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">50</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">750</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">pdf_show_xy</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;so much so that they have been known as <span style="color: #000099; font-weight: bold;">\&quot;</span>man's best friend<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">745</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// add an image under the text</span>
<span style="color: #000088;">$image</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pdf_open_image_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;jpeg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;goldenretriever.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">pdf_place_image</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #000088;">$image</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">40</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">700</span><span style="color: #339933;">,</span> <span style="color:#800080;">0.75</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// end page</span>
<span style="color: #990000;">pdf_end_page</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close and save file</span>
<span style="color: #990000;">pdf_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The next step is to locate the file through your web browser. Once you have done this, PHP will implement the script, and a new PDF file will be created and stored on your computer in the particular location listed at the top of the document.</p>
<p><em><strong>Listed are the basic steps to generating a PDF file using PHP:</strong></em></p>
<ul>
<li>First, you need to create a handle for the PDF script, which is also shown in the first line of the code above. Returning a handle to the document is done through the pdf_new() function.</li>
<li>Secondly, you have to name the file (second line of the code). This is done through the pdf_open_file() function. This requires the handle that was returned from the previous step, along with a file name that you create.</li>
<li>In the next step (third and sixth lines of the code), you are able to insert new pages into the document and end them with the pdf_begin_page() and pdf_end_page() functions. In between these two functions is the code that adds something into the document, including images, text, and geometric shapes.</li>
<li>Choosing and registering a font (fourth line of code) is done through the pdf_findfont() function, which is the selection part and requires the font name, the encoding that is to be used, and the Boolean values (the numbers or text that convert to “true”, or permit, and “false”, or not permit) and pdf_setfont(), which actually registers the font.</li>
<li>After the font is selected and registered, next is writing a line of text (fifth line). This is done through the pdf_show_xy() function. This requires a handle to the PDF file, a mention of the font type that is to be used in the document, the line of text that you want added into the document, and the X and Y coordinates, which give the points at where you want the text to start at. The coordinate numbers are based from the origin, which is located at (0,0).</li>
<li>The final step (seventh line) is to close the document, which is done through the pdf_close() function. This will also save the file to the location specified in the second step with the pdf_open_file() function and will get rid of the handle that was created in the document.</li>
</ul>
<p>The <strong>PDFLib extension</strong> will not only allow you to write text to a page, but will also enable you to insert images, which occurs through the the pdf_open_image_file() and pdf_place_image() functions and geometric shapes, such as lines and circles. The drawing of a line, for example, will happen through pdf_moveto(), pdf_lineto() and pdf_stroke() functions.</p>
<p>Another addition you can insert into your document is a <strong>grid</strong>. This code shows how to draw a rectangular grid using horizontal and vertical lines:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create new handle</span>
<span style="color: #000088;">$pdf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">pdf_new</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// open PDF file</span>
<span style="color: #990000;">pdf_open_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;my_grid.pdf&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// new page (A4)</span>
pdf_begin_page_ext<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">595</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">842</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// set a color</span>
<span style="color: #990000;">pdf_setcolor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;stroke&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rgb&quot;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// draw vertical lines</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$x</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">595</span><span style="color: #339933;">;</span> <span style="color: #000088;">$x</span><span style="color: #339933;">+=</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">pdf_moveto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">pdf_lineto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #000088;">$x</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">842</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">pdf_stroke</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// draw horizontal lines</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$y</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">&lt;=</span><span style="color: #cc66cc;">842</span><span style="color: #339933;">;</span> <span style="color: #000088;">$y</span><span style="color: #339933;">+=</span><span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">pdf_moveto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">pdf_lineto</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">595</span><span style="color: #339933;">,</span> <span style="color: #000088;">$y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">pdf_stroke</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// finish page</span>
<span style="color: #990000;">pdf_end_page</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// close and save PDF document</span>
pdf_end_document<span style="color: #009900;">&#40;</span><span style="color: #000088;">$pdf</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Once you know the steps and coding formats, creating PDF files with PHP is really quite simple and does not involve as much as some may first perceive.</p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/AFZ5QMrefao" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/php/generating-pdf-files-using-php.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/php/generating-pdf-files-using-php.html</feedburner:origLink></item>
		<item>
		<title>Using PHP to Count Active Website Users</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/Ou_mRx_6qAo/using-php-to-count-active-website-users.html</link>
		<comments>http://www.pastequestion.com/blog/php/using-php-to-count-active-website-users.html#comments</comments>
		<pubDate>Mon, 06 Jul 2009 16:28:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=75</guid>
		<description><![CDATA[This tutorial will show you two different methods to count the users currently surfing your website.]]></description>
			<content:encoded><![CDATA[<p>This tutorial will show you two different methods to count the users currently surfing your website.</p>
<p>The first method uses a MySQL table to track users, although it can be used with any database supported by PHP with appropriate modification to the scripts. Here is the structure of the table you will need to create within an existing database:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`online_users`</span> <span style="color: #66cc66;">&#40;</span>
        <span style="color: #ff0000;">`id`</span> INT<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">5</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">,</span>
        <span style="color: #ff0000;">`session`</span> VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">64</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
        <span style="color: #ff0000;">`timestamp`</span> TIMESTAMP <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #66cc66;">,</span>
        <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #66cc66;">&#40;</span>
                <span style="color: #ff0000;">`session`</span>
        <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>This table has three fields:</p>
<ul>
<li><strong>id</strong>, a counter that automatically increments for each new user that connects to the site</li>
<li><strong>session</strong>, the unique session ID of each user</li>
<li><strong>timestamp</strong>, the user’s date and time of connection</li>
</ul>
<p>The PHP code to manage the counting of users is split into two files in order to improve readability.</p>
<p>First is the connection class contained in &#8220;Mysql.class.php&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> <span style="color: #990000;">Mysql</span><span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> get_connection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">conn</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This class has a constructor in which connects to the MySQL database, and a destructor that automatically closes the connection at the end of the script. The other two methods are used to execute the queries and to get the MySQL connection that we can use in other functions.</p>
<p>Most of the work will be done by this class in &#8220;User.class.php&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User<span style="color: #009900;">&#123;</span>        
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$timer</span><span style="color: #339933;">=</span>TIMER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">=</span> <span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$timer</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> update<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UPDATE online_users SET online_users.timestamp = CURRENT_TIMESTAMP WHERE online_users.session='&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_affected_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_connection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO online_users(session) VALUES ('&quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">session</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM online_users WHERE online_users.timestamp &lt; CURRENT_TIMESTAMP - INTERVAL &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SECOND&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> count_users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) AS users FROM online_users WHERE online_users.timestamp &gt; CURRENT_TIMESTAMP - INTERVAL &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; SECOND&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'users'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The constructor&#8217;s parameters are the instance of the MySql class from the first file, and the maximum duration (in seconds) for which a user will be considered active. The default duration is specified via a constant called TIMER provided by user code.</p>
<p>The purpose of the update() method is to update or create the record in the MySQL table that corresponds to the current connection, and then to remove all rows older that the duration passed to the constructor.</p>
<p>The count_users() method returns the number of users connected to the site based on when they last connected.</p>
<p>Here is a script that we can use to test the functionality of the code, after changing the parameters passed to the Mysql constructor to appropriate values:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TIMER'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//5 minutes</span>
&nbsp;
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Mysql.class.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;User.class.php&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$db</span>   <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #990000;">Mysql</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;127.0.0.1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;root&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;users&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #000088;">$db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count_users</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The second method for counting users does not use MySQL, but instead uses PHP&#8217;s session functionality.</p>
<p>This class in &#8220;User.class.php&#8221; will do all the work:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> User<span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$timer</span><span style="color: #339933;">=</span>TIMER<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>    
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$timer</span><span style="color: #339933;">;</span>        
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> count_users<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$counter</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span>SESSION_PATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'/*'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">fileatime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">timer</span><span style="color: #009900;">&#41;</span>
                                <span style="color: #000088;">$counter</span><span style="color: #339933;">++;</span>                        
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$counter</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This script tests the functionality of the class:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'TIMER'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">300</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//5 minutes</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'SESSION_PATH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/home/user/session'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">session_save_path</span><span style="color: #009900;">&#40;</span>SESSION_PATH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> User<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count_users</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Through use of session_save_path(SESSION_PATH) we set a unique directory for storage of session information in order to prevent conflicts with other PHP applications running on the server.</p>
<p>IMPORTANT: session_save_path() MUST be called before session_start().</p>
<p>After this step we create a new instance of the User class. There is no need for an update() method since we use a quirk of PHP&#8217;s session functionality. The count_users() method checks the last access time of the session information files in order to determine how many users are currently connected.</p>
<p><strong>IMPORTANT:</strong> <em>On UNIX-like operating systems, this method will NOT work on file systems that are mounted with the noatime mount option.</em></p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/Ou_mRx_6qAo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/php/using-php-to-count-active-website-users.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/php/using-php-to-count-active-website-users.html</feedburner:origLink></item>
		<item>
		<title>How to use Python Imaging Library</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/N38KYwZWVIs/how-to-use-python-imaging-library.html</link>
		<comments>http://www.pastequestion.com/blog/python/how-to-use-python-imaging-library.html#comments</comments>
		<pubDate>Thu, 02 Jul 2009 21:25:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=56</guid>
		<description><![CDATA[The Python Imaging Libray (PIL) is a library that allows Python Programmers to open, save, and manipulate images. PIL mostly deals with image formats TIFF, JPG, BMP, PNG, and GIF, but there are even ways for a common user to add their own formats to the Library. The Python Imaging Library greatly speeds up programming time if the Python program deals with images at all.]]></description>
			<content:encoded><![CDATA[<p>The Python Imaging Libray (PIL) is a library that allows Python Programmers to open, save, and manipulate images. PIL mostly deals with image formats TIFF, JPG, BMP, PNG, and GIF, but there are even ways for a common user to add their own formats to the Library. The Python Imaging Library greatly speeds up programming time if the Python program deals with images at all.</p>
<p>To open an image with PIL, the library module merely needs to be defined, and then the Open command needs to be issued. To create an image from scratch, the &#8220;new&#8221; command must be used. Saving an image is as simple as using the &#8220;save&#8221; command. (Use &#8220;save&#8221; after modifying or creating an image).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#start out python code with the import of the Image Library</span>
<span style="color: #808080; font-style: italic;">#The Image and ImageDraw libraries are part of PIL</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> Image, ImageDraw
&nbsp;
<span style="color: #808080; font-style: italic;">#now open the mypicture.bmp image, assign the image to the variable &quot;bmp&quot;</span>
&nbsp;
bmp = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicture.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#create a new image using the &quot;new&quot; command</span>
&nbsp;
newbmp = Image.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;RGB&quot;</span>, <span style="color: black;">&#40;</span><span style="color: #ff4500;">128</span>, <span style="color: #ff4500;">128</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;White&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#save the new image</span>
&nbsp;
newbmp.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicture2.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#now, display both of the images</span>
&nbsp;
bmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
newbmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># To do more than just a block of color, there are simple commands to write whatever you want.</span>
<span style="color: #808080; font-style: italic;"># This is where the beauty of PIL comes into play.</span>
<span style="color: #808080; font-style: italic;"># To draw a red line on the black image that was just created</span>
&nbsp;
draw = ImageDraw.<span style="color: black;">Draw</span><span style="color: black;">&#40;</span>newbmp<span style="color: black;">&#41;</span>
&nbsp;
draw.<span style="color: black;">line</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>, fill=rgb<span style="color: black;">&#40;</span><span style="color: #ff4500;">255</span>,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">del</span> draw
&nbsp;
<span style="color: #808080; font-style: italic;"># And to save and see the recent handiwork</span>
&nbsp;
newbmp.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicturewithline.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
newbmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Using PIL, you can also grab a single pixel and change it. Let&#8217;s see hows that&#8217;s done&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Image, ImageDraw
&nbsp;
bmp = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicture.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
draw = ImageDraw.<span style="color: black;">Draw</span><span style="color: black;">&#40;</span>bmp <span style="color: black;">&#41;</span>
&nbsp;
draw.<span style="color: black;">point</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">64</span>,<span style="color: #ff4500;">64</span><span style="color: black;">&#41;</span>, fill=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">del</span> draw
&nbsp;
bmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>To be able to work with PIL, you only need to know a few functions. New and open will get you an image to work with, save will write your image to the disk, and show will display what you&#8217;ve done so far. A bunch of other functions exist for modifying your image once you have it open. You can draft, convert (from one file type to another), crop (cut the image to a new size), and thumbnail and resize (excellent for automatic blogging or news sites).</p>
<p>
<br/>
</p>
<p><em><strong>How to add a watermark to your images using Python Imaging Library</strong></em></p>
<p>One method involves putting one image on top of another. You will need an image of your transparent watermark handy, and then you&#8217;ll need the image you want to put it on. Then it&#8217;s just a simple process of &#8220;pasting&#8221; your watermark on the image. Let&#8217;s see how easy this process is using PIL.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Image, ImageDraw
&nbsp;
bmp= Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicture.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
mywatermark = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mywatermark.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
bmp.<span style="color: black;">paste</span><span style="color: black;">&#40;</span>mywatermark, <span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">0</span>,<span style="color: #ff4500;">100</span>,<span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
bmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Another water mark method just writes a text onto your image. You&#8217;ll need a little more doing, but it&#8217;s still really easy. (You will need a font file handy, just so you know.) In the middle of this function, you will notice a while loop. What we are doing here is testing the text size against the size of the image we want to watermark. We want the text to be a big as possible, and still remain inside the image. A little dynamic code goes a long way.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> Image, ImageDraw, ImageFont
&nbsp;
bmp = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;mypicture.bmp&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
mywatermark = Image.<span style="color: #dc143c;">new</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;RGBA&quot;</span>, <span style="color: black;">&#40;</span>bmp.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, bmp.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
drawing = ImageDraw.<span style="color: black;">ImageDraw</span><span style="color: black;">&#40;</span>mywatermark, <span style="color: #483d8b;">&quot;RGBA&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
size = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
&nbsp;
    size += <span style="color: #ff4500;">1</span>
&nbsp;
    testfont = ImageFont.<span style="color: black;">truetype</span><span style="color: black;">&#40;</span>FONT, size<span style="color: black;">&#41;</span>
&nbsp;
    testtextw, testtexth = testfont.<span style="color: black;">getsize</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;My Watermark Text&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> testtextw+testtexth/<span style="color: #ff4500;">3</span> <span style="color: #66cc66;">&gt;</span> mywatermark.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>:
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
    font = nextfont
&nbsp;
    textw, texth = testtextw, testtexth
&nbsp;
drawing.<span style="color: black;">setfont</span><span style="color: black;">&#40;</span>font<span style="color: black;">&#41;</span>
&nbsp;
drawing.<span style="color: black;">text</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>mywatermark.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>-textw<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span>, <span style="color: black;">&#40;</span>mywatermark.<span style="color: black;">size</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>-texth<span style="color: black;">&#41;</span>/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;My Watermark Text&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
bmp.<span style="color: black;">paste</span><span style="color: black;">&#40;</span>mywatermark, <span style="color: #008000;">None</span>, mywatermark<span style="color: black;">&#41;</span>
&nbsp;
bmp.<span style="color: black;">show</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Nearly any image manipulation is incredibly simple when you use the <a href="http://www.pythonware.com/products/pil/" target="_blank">Python Imaging Library</a>. This is a very well thought out program that is freely available for you to use.</p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/N38KYwZWVIs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/python/how-to-use-python-imaging-library.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/python/how-to-use-python-imaging-library.html</feedburner:origLink></item>
		<item>
		<title>Setting up a Basic Lighttpd Server</title>
		<link>http://feedproxy.google.com/~r/PasteQuestion_BLOG/~3/AkkmAFMxnVA/setting-up-a-basic-lighttpd-server.html</link>
		<comments>http://www.pastequestion.com/blog/server/setting-up-a-basic-lighttpd-server.html#comments</comments>
		<pubDate>Tue, 30 Jun 2009 09:56:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Server]]></category>

		<guid isPermaLink="false">http://www.pastequestion.com/blog/?p=7</guid>
		<description><![CDATA[Lighttpd is geared for speed, and has a smaller footprint than Apache. Having a decreased footprint is critical when the server is in high stress situations and when available hardware is scarce. Popular websites such as Youtube and Imageshack who receive great amounts of traffic each minute are served up by Lighttpd.]]></description>
			<content:encoded><![CDATA[<p>Lighttpd is geared for speed, and has a smaller footprint than Apache. Having a decreased footprint is critical when the server is in high stress situations and when available hardware is scarce. Popular websites such as Youtube and Imageshack who receive great amounts of traffic each minute are served up by Lighttpd.</p>
<p><em>How to install Lighttpd and have a basic functioning server:</em></p>
<p>First, get the newest source from <a href="http://www.lighttpd.net">http://www.lighttpd.net</a> .</p>
<p>Then login as super user:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">user<span style="color: #000000; font-weight: bold;">@</span>linux-588u:~<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #c20cb9; font-weight: bold;">su</span>
Password:
linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user <span style="color: #666666; font-style: italic;">#</span></pre></div></div>

<p>Unpack using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>desktop <span style="color: #666666; font-style: italic;">#  tar -xf /home/user/lighttpd-1.4.22.tar.gz</span></pre></div></div>

<p>Then change the directory to where the Lighttpd source got unpacked:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>desktop <span style="color: #666666; font-style: italic;">#  cd /home/user/lighttpd-1.4.22</span>
linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>lighttpd-1.4.22 <span style="color: #666666; font-style: italic;">#</span></pre></div></div>

<p>Use the configure script, compile, and install:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user<span style="color: #000000; font-weight: bold;">/</span>lighttpd-1.4.22 <span style="color: #666666; font-style: italic;">#  ./configure &amp;&amp; make &amp;&amp; make install</span></pre></div></div>

<p>Now run lighttpd:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user <span style="color: #666666; font-style: italic;">#  lighttpd</span></pre></div></div>

<p>If Lighttpd installed correctly, you should get something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000;">2009</span>-06-<span style="color: #000000;">26</span> 01:<span style="color: #000000;">35</span>:<span style="color: #000000;">52</span>: <span style="color: #7a0874; font-weight: bold;">&#40;</span>server.c.552<span style="color: #7a0874; font-weight: bold;">&#41;</span> No configuration available. Try using -foption.</pre></div></div>

<p>Ok, now that we have Lighttpd installed, we can move onto creating the config file so Lighttpd can run correctly.</p>
<p>We will create the config file in the /etc directory using vi:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user <span style="color: #666666; font-style: italic;">#  vi /etc/lighttpd.conf</span></pre></div></div>

<p>This will bring up vi where we can add the following for a basic Lighttpd server,  press the insert key then enter:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">server.document-root = <span style="color: #ff0000;">&quot;/home/user/www/htdoc&quot;</span>
&nbsp;
server.port = <span style="color: #000000;">80</span>
&nbsp;
mimetype.assign = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
  <span style="color: #ff0000;">&quot;.html&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;text/html&quot;</span>,
  <span style="color: #ff0000;">&quot;.txt&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;text/plain&quot;</span>,
  <span style="color: #ff0000;">&quot;.jpg&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;image/jpeg&quot;</span>,
  <span style="color: #ff0000;">&quot;.png&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;image/png&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
static-file.exclude-extensions = <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #ff0000;">&quot;.fcgi&quot;</span>, <span style="color: #ff0000;">&quot;.php&quot;</span>, <span style="color: #ff0000;">&quot;.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
index-file.names = <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #ff0000;">&quot;index.html&quot;</span>, <span style="color: #ff0000;">&quot;index.py&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
server.modules = <span style="color: #7a0874; font-weight: bold;">&#40;</span>
<span style="color: #ff0000;">&quot;mod_access&quot;</span>,
<span style="color: #ff0000;">&quot;mod_cgi&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
cgi.assign = <span style="color: #7a0874; font-weight: bold;">&#40;</span> <span style="color: #ff0000;">&quot;.py&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;/usr/bin/python&quot;</span>,
                   <span style="color: #ff0000;">&quot;.php&quot;</span> =<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #ff0000;">&quot;/usr/bin/cgi-php&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>After entering the config script, hit the escape key and type &#8220;wq&#8221; then enter to save and quit.</p>
<p>Now to get the server running with the conf file:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">linux-588u:<span style="color: #000000; font-weight: bold;">/</span>home<span style="color: #000000; font-weight: bold;">/</span>user <span style="color: #666666; font-style: italic;">#  lighttpd -f /etc/lighttpd.conf</span>
<span style="color: #000000;">2009</span>-06-<span style="color: #000000;">26</span> 01:<span style="color: #000000;">52</span>:<span style="color: #000000;">36</span>: <span style="color: #7a0874; font-weight: bold;">&#40;</span>log.c.97<span style="color: #7a0874; font-weight: bold;">&#41;</span> server started</pre></div></div>

<p>Congratulations, you now have a working Lighttpd setup!</p>
<p><strong>Config File Explained</strong></p>
<p>&#8220;server.document-root&#8221;</p>
<blockquote><p>Sets the document root for the server, where index.html and other files that need to be available to the web user.</p></blockquote>
<p>&#8220;server.port&#8221;</p>
<blockquote><p>Sets the port, TCP port 80 being the default port for http.</p></blockquote>
<p>&#8220;mimetype.assign&#8221;</p>
<blockquote><p>Sets the mimetype mappings.</p></blockquote>
<p>&#8220;static-file.exclude-extensions&#8221;</p>
<blockquote><p>Tells Lighttpd that these file types are handled by a cgi plugin that has been enabled. ( mod_cgi )</p></blockquote>
<p>&#8220;server.modules&#8221;</p>
<blockquote><p>Tells Lighttpd what modules to load.</p></blockquote>
<p>&#8220;cgi.assign&#8221;</p>
<blockquote><p>Tells what file types are handled by a cgi program.</p></blockquote>
<p>For further information, visit <a href="http://www.lighttpd.net">http://www.lighttpd.net</a>.</p>
<img src="http://feeds.feedburner.com/~r/PasteQuestion_BLOG/~4/AkkmAFMxnVA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.pastequestion.com/blog/server/setting-up-a-basic-lighttpd-server.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.pastequestion.com/blog/server/setting-up-a-basic-lighttpd-server.html</feedburner:origLink></item>
	</channel>
</rss>
