zoukankan      html  css  js  c++  java
  • python socket

    server

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import socket
    
    
    def main():
        host = "127.0.0.1"
        port = 5000
    
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind((host, port))
    
        print("Server started")
        while True:
            data, addr = s.recvfrom(1024)
            print("Message from: " + str(addr))
            print("From connected user: " + str(data))
            data =  str(data).upper()
            print("Sending: " + str(data))
            s.sendto(bytes(data, "utf-8"), addr)
        s.close()
    
    
    if __name__ == '__main__':
        print("socket server running...")
        main()

    client

    #!/usr/bin/env python
    # -*- coding: UTF-8 -*-
    
    import socket
    
    
    def main():
        host = "127.0.0.1"
        port = 5001
    
        server = ("127.0.0.1", 5000)
    
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind((host, port))
    
        message = input("-->")
        while message != "q":
            s.sendto(bytes(message, "utf-8"), server)
            data, addr = s.recvfrom(1024)
            print("Received from server: " + str(data))
            message = input("-->")
        s.close()
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    HelloWorld入门程序
    再写博客
    iOS--小结系列一
    iOS--ARC
    ios--block
    iOS--protocol
    iOS--内存管理
    指针基本概念
    ZooKeeper系列之十:ZooKeeper的一致性保证及Leader选举
    ZooKeeper系列之九:ZooKeeper API简介及编程
  • 原文地址:https://www.cnblogs.com/vickey-wu/p/7675172.html
Copyright © 2011-2022 走看看