Python Socket Server
½ñÌì×öftpµÄ½çÃæ,×öµÄÏ൱ÓôÃÆ£¬ÅªµÃÐÄÇé¼°Æ䲻ˬ£¬ÔÚÍøÉÏËѵ½ËÀ¶¼²»ÖªµÀ¸ÃÔõô°ì£¬´òËãÃ÷ÌìÏÈ¿´¿´C£«£«
µÄÊÇÔõôŪµÄÔÙ˵¡£²»¹ý£¬ÏÖÔÚÎÒÏëдһϹØÓÚsocketµÄ±à³Ì¡£
ÏÈдһ¸öʱ¼ä·þÎñÆ÷°É£¬Ëû¼àÌý¶Ë¿Ú£¬²¢Çһ᷵»Ø ·þÎñÆ÷µÄʱ¼ä
server.py
#!/usr/bin/python
# Copyright (c) angelipin (angelipin@126.com)
import SocketServer
import time
import logging
__version__ = "1.0.0"
class ServerHandler(SocketServer.BaseRequestHandler):
def setup(self):
logging.info("start thread.")
self.request.settimeout(60)
def finish(self):
self.request.close()
logging.info("end thread.")
def handle(self):
while 1:
try:
dataReceived = self.request.recv(1024)
if not dataReceived:
break
logging.info('Receive data '+dataReceived)
dataSend = time.ctime(time.time())
self.request.send(dataSend)
except socket.timeout:
print "Time out"
logging.error("Time out")
logging.basicConfig(level=logging.DEBUG,format='%(thread)d %(asctime)s %(levelname)s %(message)s',filename='log',filemode='a+')
Server = SocketServer.ThreadingTCPServer(('',8881),ServerHandler)
logging.info('Server start')
Server.serve_forever()
ÓÃÀ´×ö²âÊÔÓõÄclient.py
import socket
remote_host = '127.0.0.1'
remote_port = 8881
send_buf = open('binary','rb').read()
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((remote_host,remote_port))
sock.send(send_buf)
response_data = sock.recv(1024)
print response_data
sock.close()
Ëæ±ãÔÚbinaryÎļþÀïÃæдµã¶«Î÷ »òÕß ÄãÖ±½Ó»»³É¡£¡£¡£¶Ô°É
²âÊÔ ÏÈÆô¶¯ server
È»ºóÔËÐм¸¸öclient¿´¿´
while true;
do
python client.py;
done;
ÄãÒ²¿ÉÒÔд¸ö¶àÏ̵߳IJâÊÔ¡£ÏñÎÒÕâÑùµÄ¾ÍÊǶ࿪¼¸¸öshell¶¼ÔËÐÐÉÏÃæµÄ´úÂë¡£°¦£¬Ôõô½áÊøÄØ£¿
Ïà¹ØÎĵµ£º
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 029
aFile = file(r'C:\in.txt', 'r')
while True:
line = aFile.readline()
if len(line) == 0:
break
# end of if
print line,
# end of while
aFile.close()
output£º
>>>
This is the first line.
This is the second line.
This is ......
Ô´´úÂëÏÂÔØ£ºÏÂÔصØÖ·ÔÚÕâÀï
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
......
¿ÉÒÔ²¥·Å´ó²¿·ÖµÄÒôÊÓƵ.
demo download: http://www.sandy1219.com/python/media.rar
playMP3.py
# -*- coding: utf-8 -*-
import wx;
import wx.media;
import os;
import SPrint;
import mediaStateBar;
import mediaList;
import SaveLog;
import MediaItem;
woldcart = "media files|*.*|avi ......
——ÓÉÓÚ×î½üÔÚ×öÓйØÍøÒ³ËÑË÷µÄÏîÄ¿£¬Éæ¼°µ½Ò»Ð©±àÂë·½ÃæµÄ֪ʶ£¬Ð¡µÜÔÚÍøÉÏżȻµØ·¢ÏÖÁËÕâôһƪÎÄÕ£¬ºÜÒ׶®£¬²»»Þɬ£¬ÎªÁË·½±ã×Ô¼ºÒ²Í¬Ê±ÄÜ·½±ã´ó¼Ò£¬¾ÍתÁ˹ýÀ´£¬ÒÔ×÷²Î¿¼……
ÎÄÕ³ö´¦£ºhttp://blog.csdn.net/tingsking18/arc ......
ż¶û×ö¸ö¼òµ¥µÄWEB·þÎñ²âÊÔ³ÌÐò£¬ÎÒ»¹ÊÇϲ»¶²ÉÓÃPHP¡£¼ò½à£¬·ç¸ñºÍC/C++½Ó½ü£¬ÉÏÊֿ죬¶øÇÒ¿âÒ²ºÜÆëÈ«¡£ÒÔǰΪ¹ã¶«Òƶ¯×öÒƶ¯ÐÐÒµÖն˵ÄÑÝʾ³ÌÐòʱ£¬²ÉÓùúÄÚÒ»¸öÐÖµÜдµÄFleaPHP¿ªÔ´¿ò¼Ü£¬Á½Öܲ»µ½µÄʱ¼ä£¬ÎÒ´ÓÒ»¸ö¶ÔPHPÒ»ÎÞËùÖªµÄˮƽ£¬¿ìËٵĸ㶨ÁË30¶à¸öÒ³ÃæµÄºǫ́£¬ºóÀ´ÑÝʾЧ¹ûÒ²²»´í£¬Ò²ºÜÎȶ¨£¬´Ó´Ë¶ÔPHPÓÐÁ ......