zoukankan      html  css  js  c++  java
  • socket模块


    1
    #!/usr/bin/python 2 import socket,time 3 4 # Address 5 HOST = '' 6 PORT = 8000 7 8 reply = 'Yes' 9 10 # Configure socket 11 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 s.bind((HOST, PORT)) 13 14 # passively wait, 3: maximum number of connections in the queue 15 s.listen(3) 16 #conn, addr = s.accept() 17 while True: 18 conn, addr = s.accept() 19 # receive message 20 request = conn.recv(1024) 21 print 'request is: ',request 22 print 'Connected by', addr 23 time.sleep(15) 24 request = conn.recv(1024) 25 print 'request is: ',request 26 # send message 27 conn.sendall(reply) 28 # close connection 29 conn.close()

    客户端:

     1 import socket,time
     2 
     3 # Address
     4 HOST = '127.0.0.1'
     5 PORT = 8000
     6 
     7 request = 'can you hear me?'
     8 
     9 # configure socket
    10 s       = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    11 s.connect((HOST, PORT))
    12 
    13 # send message
    14 s.sendall(request)
    15 s.sendall(request)
    16 s.sendall(request)
    17 #time.sleep
    18 s.sendall(request)
    19 s.sendall(request)
    20 # receive message
    21 #reply   = s.recv(1024)
    22 #print 'reply is: ',reply
    23 #time.sleep(15)
    24 # close connection
    25 s.close()
  • 相关阅读:
    gtest(C++单元测试框架)
    tinyXML入门
    笔记 解决vue3动态绑定本地图片失效问题
    面试技巧
    vuex 状态管理
    插槽的使用
    Vue-router 路由
    Vue组件
    (转)JS 常用 DOM
    9-26
  • 原文地址:https://www.cnblogs.com/saolv/p/6862666.html
Copyright © 2011-2022 走看看