python socket编程

需要在多台服务器上运行日志分析脚本,分析完成后每台机器直接发送邮件会出现大量邮件同时过来,现在想将多台机器的分析结果收集起来,通过网络发送到服务端,在服务端收集所有的日志,在统一发送一封邮件,实现的socket代码如下。

接收端(server)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
import socket
def socket_server(host,port):
    self_host = host
    self_port = port

    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((self_host,self_port))
    s.listen(4)

    f = open("log", "a")
    while True:
        conn,addr=s.accept()
        data = conn.recv(1024)
        print 'data:', data
        f.write(data)
    f.close()
    s.close()

if __name__ == "__main__":
    host = '127.0.0.1'
    port = 65530

    socket_server(host,port)

发送端(client)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
import socket

def socket_client(host,port,content):
    self_host = host
    self_port = port
    self_content = content

    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect((self_host,self_port))
    s.sendall(content)
    s.close()

if __name__ == "__main__":
    host = "127.0.0.1"
    port = 65530

    f = open("sendfile","r")
    content = f.read()
    f.close()

    socket_client(host, port, content)