Python中socket和多线程的应用

这是我以前写的一个socket小应用,其中也顺便用到了一点点多线程的东西。
这个socket客户端是我为了方便公司的充值系统调试而写的一个小应用。由它跟充值系统进行socket通讯,自动完成充值的测试工作。
看代码吧:
import socket,thread

def Communicate(s):
    phoneno = raw_input("Please input your PhoneNo.:(Push Enter to use default)")   #Get local phoneno
    if (len(phoneno)) == 0:
        phoneno= '123456'
    phoneno="phone:"+phoneno    #Add a prefix of phoneno
    s.send(phoneno) #Send data string to server
    print '>> %s'%phoneno
    data = s.recv(4096) #Get response data from server
    print '<< %s'%data     #0819022324
    process_string='*571253*0861235468*1*2617*3#'
    s.send(process_string)
    print '>> %s'%process_string
    print '<< %s'%s.recv(4096)
    s.send('6506')
    print '>> %s'%'6506'
    print '<< %s'%s.recv(4096)
    s.close()   #Disconnect the socket connection
    print 'End!'    #End the program
    
def Refill():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #Create a socket
    try:
        #s.connect((socket.gethostname(),  8888))    #Connect to target server
        s.connect(('192.168.4.129',  8888))    #Connect to target server
    except:
        print "Connect to '%s' failed!"%(socket.gethostname())
    else:
        #Communicate(s)
        thread.start_new_thread(Communicate,(s,))   #Socket communicate on a new thread. 

print "-----------------------------------------------------------"
print "Welcom to use Python Socket Tools.SocketTest.py/nThis program is designed to test FRS4 socket communicate test/n----Powered by Lucker."
if(__name__=='__main__'):
    Refill()



 
《完》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值