Python Web¿ª·¢ÓëApache2+mod_wsgi²¿Êð
×î½üÔÚ¿´Python Web·½ÃæµÄ¿ª·¢£¬³õ²½½Ó´¥ÁËDjangoºÍweb.py(×¢:ºÍweb2pyÍêÈ«²»Ïà¹Ø)Á½¸ö¿ò¼Ü¡£
Èç¹û½áºÏApache²¿ÊðÐèҪһЩÑо¿£¬¿ÉÄܶÔCGIµÄ²»ÊìϤ°É¡£ËäÈ»mod_pythonÒ²¿ÉÒÔ£¬µ«Ëƺõ¸üÁ÷ÐÐmod_wsgi
,ÎÒÊÇÔÚUbuntu°²×°µÄ,±¾À´ÏëÔ´Âë±àÒëµÄ£¬¿ÉÊÇmakeµÄʱºò×ÜÊDZ¨´í£¬×îºó$sudo apt-get install libapache2-mod-wsgi ,µ¹Ò²¼òµ¥¡£
×¢:Èç¹û²ÉÓñàÒëµÄ·½Ê½£¬ÐèҪԤװapxs2(apxs is the tool for building modules for Apache (apxs2 is for apache2),
$sudo apt-get install apache2-threaded-dev
×°ºÃ¾ÍÊÇÐÞ¸ÄÅäÖÃÎļþÁË,Ö÷ÒªÊÇÐÞ¸Ä/etc/apache2/sites-available/defaultÎļþ:
»ù±¾ÕÕ×ÅÏòµ¼
À´¾Í¿ÉÒÔ£¬ÒòΪÊÇÓ¢Îĵģ¬ÕâÀï¼òÒª½éÉÜÈçÏÂ:
ÔÚVirtualHostÄÚÌí¼Ó
#²ÉÓÃÊØ»¤Ä£Ê½¶ø·ÇÄÚǶģʽ
WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
££´´½¨python½Å±¾Ö´ÐеıðÃû,ÍÆ¼ö³ÌÐò½Å±¾·ÅÔÚ¶ÀÁ¢µÄĿ¼¶ø·ÇDocumentRoot
WSGIScriptAlias /mywebpy /usr/local/mywebpy/index.py
<Directory /usr/local/mywebpy>
Order allow,deny
Allow from all
</Directory>
web.pyʵÀý´úÂëÈçÏÂ:
#!/usr/bin/env python
import web
urls = (
'/(.*)', 'hello'
)
class hello:
def GET(self, name):
i = web.input(times=1)
if not name: name = 'world'
for c in xrange(int(i.times)): print'Hello,', name+'!'
application = web.application(urls, globals()).wsgifunc()
µ½ÕâÀï,»¹ÓÐÒ»¸öÎÊÌ⣬ÄǾÍÊÇ´úÂëÖеÄprintÓï¾ä²¢²»±»mod_wsgiÖ§³Ö£¬Äã¿ÉÄܻῴµ½internal errorÖ®ÀàµÄÐÅÏ¢£¬Í¨¹ý/var/log/apache2/error.og¿ÉÒÔ¿´µ½ÏêϸµÄÐÅÏ¢£¬ÄÇÊÇÒòΪ±ê×¼ÊäÈë/Êä³öµÄÓï¾äÔÚWSGIÖÐÊDz»ÔÊÐíµÄ£¬ÔÒòÈçÏ£º
Q: Why do I get the error 'IOError: sys.stdout access restricted by mod_wsgi'?
A: A portable WSGI application or application component should not output anything to standard output. This is because some WSGI hosting mechanisms use standard output to communicate with the web server. If a WSGI application outputs anything to standard outpu
Ïà¹ØÎĵµ£º
"""A parser for HTML and XHTML."""
# This file is based on sgmllib.py, but the API is slightly different.
# XXX There should be a way to distinguish between PCDATA (parsed
# character data -- the normal case), RCDATA (replaceable character
# data -- only char and entity references and end tags a ......
http://blog.alexa-pro.cn/?p=197
1.´ò¿ªÎļþ´úÂëÈçÏ£º
>>> f = open(”d:test.txt”, “w”)
˵Ã÷£ºµÚÒ»¸ö²ÎÊýÊÇÎļþÃû³Æ£¬°üÀ¨Â·¾¶£»µÚ¶þ¸ö²ÎÊýÊÇ´ò¿ªµÄģʽmode’r'£ºÖ»¶Á£¨È±Ê¡¡£Èç¹ûÎļþ²»´æÔÚ£¬ÔòÅ׳ö´íÎ󣩒w'£ºÖ»Ð´£¨Èç¹ûÎļþ ²»´æÔÚ£¬Ôò×Ô¶¯´´½¨Îļþ£©’a'£º¸½¼ ......
server:
import sys
import socket
host = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
while 1:
try:
&n ......
ÔÚ»ùÓÚ»¥ÁªÍøµÄÓ¦ÓÃÖУ¬³ÌÐò¾³£ÐèÒª×Ô¶¯µØ·¢Ë͵ç×ÓÓʼþ¡£È磺һ¸öÍøÕ¾µÄ×¢²áϵͳ»áÔÚÓû§×¢²áʱ·¢ËÍÒ»·âÓʼþÀ´È·ÈÏ×¢²á£»µ±Óû§Íü¼ÇµÇ½ÃÜÂëµÄʱºò£¬Í¨¹ýÓʼþÀ´È¡»ØÃÜÂë¡£smtplibÄ£¿éÊÇpythonÖÐsmtp(¼òµ¥Óʼþ´«ÊäÐÒé)µÄ¿Í»§¶ËʵÏÖ¡£ÎÒÃÇ¿ÉÒÔʹÓÃsmtplibÄ£¿é£¬ÇáËɵķ¢Ë͵ç×ÓÓʼþ¡£ÏÂÃæµÄÀý×ÓÓÃÁ˲»µ½Ê®ÐдúÂëÀ´·¢Ë͵ç×ÓÓÊ ......
python 2.4ÖÐdatetimeÓÐstrftime·½·¨¶øÎÞstrptime·½·¨
¶øpython2.5ÖÐÕâÁ½¸ö·½·¨¾ùÓУ¬¶øÎҵĿª·¢»·¾³ÕýºÃÊÇpython 2.5£¬¶øÔËÐл·¾³ÔòÊÇpython 2.4
¿ª·¢»·¾³Ïµ÷ÊԺõijÌÐò£¬ÔÚ·þÎñÆ÷ÉϾͲ»run¡£²éÁËÒ»ÏÂpythonµÄ¹Ù·½Îĵµ
£¬Ð±Ìåд×Å£ºNew
in version 2.5.
²»¼æÈݵĴúÂëÈçÏ£º
PythonÓïÑÔ
:
test_strptime.py
res ......