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
 
Ïà¹ØÎĵµ£º
ʹÓÃpdbµ÷ÊÔPython³ÌÐò
±¾ÎÄÌÖÂÛÔÚûÓз½±ãµÄIDE¹¤¾ß¿ÉÓõÄÇé¿öÏ£¬Ê¹ÓÃpdbµ÷ÊÔpython³ÌÐò
Ô´ÂëÀý×Ó
ÀýÈ磬ÓÐÄ£Äâ˰ÊÕ¼ÆËãµÄ³ÌÐò£º
#!/usr/bin/python
def debug_demo(val):
if val &l ......
http://blog.alexa-pro.cn/?p=197
1.´ò¿ªÎļþ´úÂëÈçÏ£º
>>> f = open(”d:test.txt”, “w”)
˵Ã÷£ºµÚÒ»¸ö²ÎÊýÊÇÎļþÃû³Æ£¬°üÀ¨Â·¾¶£»µÚ¶þ¸ö²ÎÊýÊÇ´ò¿ªµÄģʽmode’r'£ºÖ»¶Á£¨È±Ê¡¡£Èç¹ûÎļþ²»´æÔÚ£¬ÔòÅ׳ö´íÎ󣩒w'£ºÖ»Ð´£¨Èç¹ûÎļþ ²»´æÔÚ£¬Ôò×Ô¶¯´´½¨Îļþ£©’a'£º¸½¼ ......
ÿ¸öÄ£¿é¶¼ÓÐÒ»¸öÃû³Æ£¬ÔÚÄ£¿éÖпÉÒÔͨ¹ýÓï¾äÀ´ÕÒ³öÄ£¿éµÄÃû³Æ¡£ÕâÔÚÒ»¸ö³¡ºÏÌØ±ðÓÐÓÃ
——¾ÍÈçÇ°ÃæËùÌáµ½µÄ£¬µ±Ò»¸öÄ£¿é±»µÚÒ»´ÎÊäÈëµÄʱºò£¬Õâ¸öÄ£¿éµÄÖ÷¿é½«±»ÔËÐС£
ÿ¸öPythonÄ£¿é¶¼ÓÐËüµÄ__name__£¬Èç¹ûËüÊÇ'__main__'£¬Õâ˵Ã÷Õâ¸öÄ£¿é±»Óû§µ¥¶ÀÔËÐУ¬
ÎÒÃÇ¿ÉÒÔ½øÐÐÏàÓ¦µÄÇ¡µ±²Ù×÷¡£
#!/usr/bin/pytho ......
ãÂã¶®¶®µÄ°²×°ÁËPython£¬²»Îª±ðµÄ£¬Ö»Îª¸Ïµãʱ÷Ö£¬ÒòΪ¿´¼ûºÜ¶àÓÎÏ·¹«Ë¾¶¼ÒªÇó»áÒ»µãPython»òÕßLUA£¬Ã»°ì·¨£¬»¹ÊǶàѧһµãºÃ£¬·´ÕýÏÐ×ÅÒ²ÊÇÏÐ×Å¡£×Ô¼ºÄ¿Ç°²»¶®LINUX£¬ËùÒÔÔÝÇÒÓÃÓÃWINDOWSϵİɡ£
ÈëÃÅ»¹ºÃÈëÊÖ£¬²»ÊǾÞÄÑ£¬Ö¸³ö¼¸µãÓÐÒâ˼µÄ£º
ûÓлù±¾ÀàÐÍÁË£¬µ±ÎÒ°²cÓïÑÔÓ﷨дint xÊ ......