Python 輸出文件內容到網絡端口
阿新 • • 發佈:2017-10-29
bsp __name__ 輸出 [1] exit send cond lee line 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)
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])
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 輸出文件內容到網絡端口