zoukankan      html  css  js  c++  java
  • Python socket(TCP阻塞模式)基础程式

    前置知识:Python基础语法,socket库


    tips:

    1. 默认HOST_IP:127.0.0.1

    2. 默认HOST_PORT:7676


    参考代码:


    1. 客户端程式


    #!/usr/bin/env python
    # encoding: utf-8
    import socket
    import os
    import sys
    import time
    import traceback
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    def SocketInit():
    	try:
    		s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    	except Exception as e:
    		print "[-]Socket Create Error"
    		sys.exit(-1)
    	else:
    		print "[*]Socket Create Success"
    	return s
    
    def Conn(s,host,port):
    	while 1:
    		try:
    			s.connect((host,port))
    		except Exception as e:
    			print "[-]Socket Connect Error"
    			#traceback.print_exc()
    			time.sleep(2)
    			#sys.exit(-1)
    		else:
    			print "[*]Socket Connect Success"
    			break
    
    def main():
    	host = "127.0.0.1"
    	port = 7676
    	sC = SocketInit()
    	Conn(sC, host, port)
    	print sC.recv(1024)
    	try:
    		while 1:
    			Str_buffer = raw_input(host+":"+str(port)+">")
    			try:
    				sC.send(Str_buffer)
    			except Exception as e:
    				print "[-]Can not send Data"
    			else:
    				recv_buffer = sC.recv(1024)
    				if not recv_buffer:
    					print "[-]Server is Off"
    				else:
    					print recv_buffer
    	except KeyboardInterrupt:
    		sC.close()
    		sys.exit(0)
    
    if __name__ == '__main__':
    	main()



    2. 服务端程式


    #!/usr/bin/env python
    # encoding: utf-8
    import socket
    import os
    import sys
    import time
    
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
    def SocketInit():
    	try:
    		s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    	except Exception as e:
    		print "[-]Socket Create Error"
    		sys.exit(-1)
    	else:
    		print "[*]Socket Create Success"
    	return s
    
    def BindSocket(s,host,port):
    	try:
    		s.bind((host,port))
    	except Exception as e:
    		print "[-]Socket Bind Error"
    		sys.exit(-1)
    	else:
    		print "[*]Socket Bind Success"
    
    def main():
    	host = "127.0.0.1"
    	port = 7676
    	sS = SocketInit()
    	BindSocket(sS, host, port)
    	sS.listen(1)
    
    	try:
    		print "[+]Waiting for Connect ..."
    		sC,addr = sS.accept()
    	except Exception as e:
    		print "[-]Server Accept Error"
    		sys.exit(-1)
    	sC.send("[*]Welcome to PeterZ-Server")
    
    	print "[*]Connect to %s:%d" % (addr[0],addr[1])
    
    	while 1:
    		try:
    			Str_buffer = sC.recv(1024)
    			Send_buffer = "%s:%s(%s)>%s" % (addr[0],addr[1],time.ctime(),Str_buffer)
    			sC.send(Send_buffer)
    			print Send_buffer
    		except KeyboardInterrupt:
    			sS.close()
    			sC.close()
    			sys.exit(0)
    		except Exception:
    			print "[-]Client is down"
    			sys.exit(0)
    
    if __name__ == '__main__':
    	main()


  • 相关阅读:
    Vue 中使用 viewerjs
    PS 给照片换背景
    HTML学习-1网页基础知识
    git使用
    Java读取XML配置文件
    Java是如何读到hbase-site.xml 的内容的
    HBASE count方法总结
    Getting Started with Java Development on Docker
    持续集成案例学习:Docker、Java与Maven
    利用MAVEN打包时,如何包含更多的资源文件
  • 原文地址:https://www.cnblogs.com/csnd/p/12897050.html
Copyright © 2011-2022 走看看