zoukankan      html  css  js  c++  java
  • Python网络编程(Sockets)

    一个简单的服务器

    #!/usr/bin/python3           # This is server.py file
    import socket
    
    # create a socket object
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    
    # get local machine name
    host = socket.gethostname()                           
    
    port = 8088
    
    # bind to the port
    serversocket.bind((host, port))                                  
    print("Server start at port: 8088")
    # queue up to 5 requests
    serversocket.listen(5)                                           
    
    while True:
        # establish a connection
        clientsocket,addr = serversocket.accept()      
    
        print("Got a connection from %s" % str(addr))
    
        msg='Thank you for connecting'+ "
    "
        clientsocket.send(msg.encode('ascii'))
        clientsocket.close()

    一个简单的客户端

    #!/usr/bin/python3           # This is client.py file
    
    import socket
    
    # create a socket object
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    
    # get local machine name
    host = socket.gethostname()
    
    port = 8088
    
    # connection to hostname on the port.
    s.connect((host, port))
    
    # Receive no more than 1024 bytes
    msg = s.recv(1024)
    
    s.close()
    
    print (msg.decode('ascii'))

  • 相关阅读:
    线性代数回顾+深化(未完成版)
    HIT OS2020 Spring Lab2
    选择
    工业互联网
    leetcode-200 岛屿数量
    记网易面试题<二>
    记网易面试题《一》
    leetecode-14-最长公共子串-简单
    leetcode-1012 至少有1位重复的数字
    协程
  • 原文地址:https://www.cnblogs.com/sea-stream/p/10181097.html
Copyright © 2011-2022 走看看