python笔记(一)面向对象

用python语言可以轻松构建一个网络应用,我把python语言定义为把玩,应用扩展,用它来构建网络应用确实比较轻松啊。比如说发邮件吧,在C++下邮件发送一直找不到的开源包,dephi下indy也是不能平台移植。如果用c嵌入python语言就变的非常简单了......

-------------------------------------------------------------------------

 

# -*- coding: cp936 -*-
###################################
#
#       2009-09-16
#   
###################################
#导入工具包
import socket,struct,time,re

#定义对象
class TestClient:

    #构造函数、也可以说入口函数
    def __init__(self):
        self.Sock = None
        self.ServerIp= 'localhost'
        self.ServerPort = 5150
        self.timeout = 3

    def SessionWithServer(self):
        error = self.Sock.connect((self.ServerIp,self.ServerPort))
        if error != None:
           print('connect errror:',error)
        else:
            while 1:
               self.Sock.send('Hello world !')
               print 'send:','Hello world !'
               revdata = self.Sock.recv(1024)
               print 'received:',revdata
               time.sleep(1)
          
    def setServerAddr(self,ip,port):
        self.ServerIp=ip
        self.ServerPort = port

    def getServerAddr(self):
        return (self.ServerIp,self.ServerPort)

    def setTimeout(self,To):
        self.timeout = To

    def getTimeout(self):
        return self.timeout

    def createSocket(self):
        self.Sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        self.Sock.settimeout(self.timeout)

    def close(self):
        self.Sock.close()
#main方法
if __name__ == '__main__':
    tc = TestClient()
    #tc.setServerAddr('10.8.10.94',5150)
    tc.createSocket()
    tc.SessionWithServer()
    tc.close()
   

你可能感兴趣的:(C++,c,python,socket,网络应用)