【Python基础】Python 输出文件内容到网络端口

Python 输出文件内容到网络端口

返回: Python基础 索引页

$ cat mySocketTest.py

 

import sys
import time
import socket

if __name__ == "__main__":
if len(sys.argv) < 4:
print >> sys.stderr, "Usage: mySocketTest.py <host> <port> <lines-per-second> <files>"
exit(-1)

host = sys.argv[1]
port = int(sys.argv[2])
sleeptime = 1/float(sys.argv[3])
filelist = sys.argv[4:]

mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysocket.bind((host,port))
mysocket.listen(1)

while(1):
   (clientsocket,address) = mysocket.accept()

   for filename in filelist:

       print "Sending",filename

       for line in open(filename):

            print line
            clientsocket.send(line)
            time.sleep(sleeptime)

 

返回: Python基础 索引页

posted @ 2017-10-29 21:16  健哥的数据花园  阅读(1190)  评论(0编辑  收藏  举报