This is a small Socket Application I wrote before, and I also used a little bit of multithreading.
This Socket Client is a small application I wrote to facilitate the company's recharge system debugging. It communicates with the recharge system to automatically complete the recharge test.
ViewCodeRight:
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 = '000000' phoneno =" Phone: "+ Phoneno # Add Prefix Of phoneno S. Send (phoneno) # SendData 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 ('20140901 ') 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 ('2017. 168.4.129 ', 8888) # connect to target server failed t: Print " Connect to '% s' failed! "% (Socket. gethostname ()) Else : # Communicate (s) thread. start_new_thread (communicate, (S,) # socket communicate onNew 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 ()
Conclusion