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

时间:2022-06-03 14:31:37

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)

$