python socket server ²¢·¢£¨×ª£©
import SocketServer, time, select, sys
from threading import Thread
COMMAND_HELLO = 1
COMMAND_QUIT = 2
# The SimpleRequestHandler class uses this to parse command lines.
class SimpleCommandProcessor:
def __init__(self):
pass
def process(self, line, request):
"""Process a command"""
args = line.split(' ')
command = args[0].lower()
args = args[1:]
if command == 'hello':
request.send('HELLO TO YOU TO!\n\r')
return COMMAND_HELLO
elif command == 'quit':
request.send('OK, SEE YOU LATER\n\r')
return COMMAND_QUIT
else:
request.send('Unknown command: "%s"\n\r' % command)
# SimpleServer extends the TCPServer, using the threading mix in
# to create a new thread for every request.
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
# This means the main server will not do the equivalent of a
# pthread_join() on the new threads. With this set, Ctrl-C will
# kill the server reliably.
daemon_threads = True
# By setting this we allow the server to re-bind to the address by
# setting SO_REUSEADDR, meaning you don't have to wait for
# timeouts when you kill the server and the sockets don't get
 
Ïà¹ØÎĵµ£º
"""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 ......
from http://blog.alexa-pro.cn/?p=189
1: os.listdir(path) //pathΪĿ¼
¹¦ÄÜÏ൱ÓÚÔÚpathĿ¼ÏÂÖ´ÐÐdirÃüÁ·µ»ØÎªlistÀàÐÍ
print os.listdir('..')
Êä³ö£º
[a,b,c,d]
2: os.path.walk(path,visit,arg)
path £ºÊǽ«Òª±éÀúµÄĿ¼
visit £ºÊÇÒ»¸öº¯ÊýÖ¸Õ룬º¯ÊýÔ²ÐÎΪ£º
callback(arg,dir,fileList)
ÆäÖÐargΪΠ......
http://blog.alexa-pro.cn/?p=197
1.´ò¿ªÎļþ´úÂëÈçÏ£º
>>> f = open(”d:test.txt”, “w”)
˵Ã÷£ºµÚÒ»¸ö²ÎÊýÊÇÎļþÃû³Æ£¬°üÀ¨Â·¾¶£»µÚ¶þ¸ö²ÎÊýÊÇ´ò¿ªµÄģʽmode’r'£ºÖ»¶Á£¨È±Ê¡¡£Èç¹ûÎļþ²»´æÔÚ£¬ÔòÅ׳ö´íÎ󣩒w'£ºÖ»Ð´£¨Èç¹ûÎļþ ²»´æÔÚ£¬Ôò×Ô¶¯´´½¨Îļþ£©’a'£º¸½¼ ......
self
ÀàµÄ·½·¨ÓëÆÕͨµÄº¯ÊýÖ»ÓÐÒ»¸öÌØ±ðµÄÇø±ð——ËüÃDZØÐëÓÐÒ»¸ö¶îÍâµÄµÚÒ»¸ö²ÎÊýÃû³Æ£¬µ«ÊÇ
ÔÚµ÷ÓÃÕâ¸ö·½·¨µÄʱºòÄ㲻ΪÕâ¸ö²ÎÊý¸³Öµ£¬Python»áÌṩÕâ¸öÖµ¡£Õâ¸öÌØ±ðµÄ±äÁ¿Ö¸¶ÔÏó±¾
Éí£¬°´ÕÕ¹ßÀýËüµÄÃû³ÆÊÇself¡£
ÕâÒ²Òâζ×ÅÈç¹ûÄãÓÐÒ»¸ö²»ÐèÒª²ÎÊýµÄ·½·¨£¬Ä㻹ÊǵøøÕâ¸ö·½·¨¶¨ÒåÒ»¸öself²ÎÊý¡£
__init_ ......
OS¿âÌṩÁËʹÓø÷ÖÖ²Ù×÷ϵͳ¹¦ÄܵĽӿڡ£
os.name ·µ»Øµ±Ç°²Ù×÷ϵͳÃû£¬¶¨ÒåÁË'posix'£¬'nt'£¬'mac'£¬'os2'£¬'ce'£¬'java'£¨ÎÒʹÓÃwin7/python3.1,Êä³öµÄÊÇnt¡£posix²»ÖªµÀÊÇʲôϵͳ£¬macÓ¦¸ÃÊÇApple»ú°É£¬os2Ò²²»ÖªµÀÊÇʲô¶«¶«£¬ceÓ¦¸ÃÊÇwinCE°É£¬ÕâÑùµÄ»°£¬pythonÓ¦¸ÃÖ§³ÖÏòCEÒÆÖ²°¡£¬Å¼×Ô¼ºÊÔ¹ý£¬Ã»±àÒë³É¹¦£¬java¹À¼ ......