程序员

Python socket服务器端、客户端传送信息

2017-10-16  本文已影响0人  是阿离

问题

windows环境主机A上想每隔15分钟获取远程Linux主机B上定时监控的logFile文件,但通过在A主机上:telnet B'ip port (例:telnet 158.123.12.1 21)查看远程主机没开放2122端口,好像不能使用ftp或者sftp来获取(应该是不可以吧)。想自己写个简单服务器实现:
1、服务器端能够正确读取logFile内容
2、客户端访问时,服务器端能够返回logFile的内容
2、客户端能够正确获取logFile内容并写入本地文件中

解决

使用Python的socket模块,自己新增一个可以直接访问的PORT。为了保证数据准确性,使用比较简单的方法:客户端与服务器端通过TCP方式通信。服务器端先启动后,循环等待客户端访问。由于是单个客户端每隔15分钟访问,每完成一次访问便断开连接,所以采用了单线程阻塞的方法实现的服务器端:

远程Linux主机B 服务器端代码:
    #coding=utf-8
    #!/usr/bin/python
    
    from socket import *
    
    HOST = '158.123.12.1'
    PORT = 8083
    BUFSIZE = 65535
    ADDR = (HOST, PORT)
    
    tcpSerSock = socket(AF_INET, SOCK_STREAM)
    tcpSerSock.bind(ADDR)
    tcpSerSock.listen(5)
    
    welcomeStr = 'Welcome to 12.1 python socket server'
    
    def logFileRead(logFile):
        '''
        Read logFile by line
        return List
        '''
        logFileDealList = []
        with open(logFile, 'r') as logFileContent:
            for line in logFileContent.readlines():
                logFileDealList.append(line)
        return logFileDealList
    
    if __name__ == "__main__":
        fileDir = 'filePath/warningMessage.txt'
    
        while True:
            print 'Enter 12.1 python socket server'
            tcpCliSock, addr = tcpSerSock.accept()
            print 'Connected from : ', addr
            data = tcpCliSock.recv(BUFSIZE)
    
            if not data:
                break
            print data
            logFileContentList = logFileRead(fileDir)
            # print logFileContentList
            for fileContent in logFileContentList:
                if fileContent.find('pending') == -1:
                    continue
                tcpCliSock.send('%s' % fileContent)
            tcpCliSock.close()
    
        tcpSerSock.close()
本地Windows主机A 客户端代码:
    #coding=utf-8
    #!/usr/bin/python
    
    from socket import *
    
    HOST = '158.123.12.1'
    PORT = 8083
    BUFSIZE = 65535
    ADDR = (HOST, PORT)
    
    welcomeStr = 'Welcome to 12.1 python socket server'
    
    def fileWrite(record, fileName):
         with open(fileName, 'w') as logFile:
            for recordItem in record:
                logFile.write(recordItem)
    
    def main():
        tcpCliSock = socket(AF_INET, SOCK_STREAM)
        tcpCliSock.connect(ADDR)
        fileDir = 'filePath/warnLogFile.txt'
        print 'Will connect 12.1 python socket server'
        data = 'hello'
        tcpCliSock.send(data)
    
        retDataAll = ''
    
        while True:
            retDataTmp = tcpCliSock.recv(BUFSIZE)
            if not retDataTmp:
                break
            if not len(retDataTmp):
                break
            print retDataTmp
            retDataAll = retDataAll + retDataTmp
        print 'end '
        tcpCliSock.close()
        fileWrite(retDataAll, fileDir)
    
    if __name__ == '__main__':
        main()

参考文章

Python核心编程(第3版)
TCP握手与socket通信细节
Python中使用socket发送HTTP请求数据接收不完整问题解决方法

上一篇下一篇

猜你喜欢

热点阅读