zoukankan      html  css  js  c++  java
  • Python实现TCP通讯

    Environment

    Client:Windows
    Server:KaLi Linux(VM_virtul)
    Network:Same LAN

    Client

    #!/usr/bin/python3
    #-*- coding: utf-8 -*-
    #@Time : 2021/3/26 23:22
    #@Author : HUGBOY
    #@File : TCPClient.py.py
    #@Software: PyCharm
    
    import socket
    
    clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    #host = '192.168.1.xxx'
    host = socket.gethostname()
    
    port = 444
    
    clientsocket.connect(('192.168.1.xxx', port))
    
    message = clientsocket.recv(1024)
    
    clientsocket.close()
    
    print(message.decode('ascii'))
    

    Server

    #!/usr/bin/python3
    #-*- coding: utf-8 -*-
    #@Time : 2021/3/26 22:54
    #@Author : HUGBOY
    #@File : server.py.py
    #@Software: PyCharm
    
    import socket
    
    #Creating the socket object
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    
    #ip = '192.168.1.xxx'
    #host = socket.gethostbyname()
    
    host = '192.168.1.xxx'
    port = 444
    
    #Binding to socket
    serversocket.bind((host, port))
    
    #Starting TCP listener
    serversocket.listen(3)
    
    while True:
        #Staring the connection
        clientsocket, address = serversocket.accept()
    
        print("received connection from %s " % str(address))
    
        message = "Hello,I am TCPServer thanks to your connect."
        message = input('Reply:')
    
        clientsocket.send(message.encode('ascii'))
    
        clientsocket.close()
    
    

    Effect

    1.Client

    2.Server

    3.Client


    ________________________________________________________

    Every good deed you do will someday come back to you.

    Love you,love word !
  • 相关阅读:
    10.31JS日记
    10.24JS日记
    10.23JS日记
    10.22JS日记
    10.19JS日记
    10.18JS日记
    Tomcat—Bad Request
    2016年上半年总结
    线程间操作无效
    压缩字符串的函数
  • 原文地址:https://www.cnblogs.com/hugboy/p/Python_TCP.html
Copyright © 2011-2022 走看看