1. 程式人生 > >Python 輸出文件內容到網絡端口

Python 輸出文件內容到網絡端口

bsp __name__ 輸出 [1] exit send cond lee line

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 輸出文件內容到網絡端口