Day 17-ネットワークエンジニアリング

5449 ワード

1.
"""
socket  (   ):                    (       )
socket      TCP UDP                      

python      socket    socket  
"""

import socket

#   socket      

if __name__ == '__main__':
    # 1.           
    """
    family:       
            AF_INET --> ipv4
            AF_INET6 --> ipv6
            
    type:        
        SOCK_STREAM -> TCP  
        SOCK_DGRAM -> UDP   
    """
    # server = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
    server = socket.socket()

    # 2.   ip     
    """
        :(ip     ,    )
                       ,   0-65535;
      :1.  0-1024 '    '            ,     。
         2.                
    """
    server.bind(('10.7.181.117', 8081))

    # 3.   
    """
      :                   
    """
    server.listen(50)
    print('      ')

    #               
    while True:
        # 4.         
        """
        accept()     ,               ,      
        """
        client, addr = server.accept()
        print(addr)

        # 5.        
        """
        send(  ):          (bytes)  
        
               :
        bytes(   ,encoding=    )
           .encode(encoding=    )
        """
        client.send('HTTP/1.1 200 OK\r
\r
'.encode(encoding='utf-8')) client.send(bytes('hello', encoding='utf-8')) client.send('python'.encode()) # """ bufsize: ( ) 1024 -> 1k 1024k -> 1M 1024M -> 1G 1024G -> 1T 1024T - 1P (bytes) : a. str( , encoding='utf-8') b. .decode(encoding='utf-8') """ # data = client.recv(1024) # str1 = str(data, encoding='utf-8') # str2 = data.decode(encoding='utf-8') # print(str1) # 6. client.close()

2.
import socket

if __name__ == '__main__':
    # 1.       
    client = socket.socket()

    # 2.     
    client.connect(('10.7.181.117', 8081))

    # 3.    
    data = client.recv(1024)
    print('         :', data.decode(encoding='utf-8'))


    # 4.    
    str1 = input('>>>')
    client.send(str1.encode(encoding='utf-8'))

    client.close()

3.
import socket

if __name__ == '__main__':
    # 1.    
    server = socket.socket()

    # 2.    
    server.bind(('10.7.181.117', 12345))

    # 3.  
    server.listen(50)

    #         
    while True:
        conversation, addr = server.accept()
        print(addr)

        #                  
        while True:
            #     
            message = input('>>>')
            conversation.send(message.encode())

            #     
            message_data = conversation.recv(1024)
            print(message_data.decode(encoding='utf-8'))

4.
import socket
if __name__ == '__main__':
    # 1.       
    client = socket.socket()

    # 2.     
    client.connect(('10.7.181.117', 12345))

    while True:
        #     
        data = client.recv(1024)
        print(data.decode(encoding='utf-8'))

        #     
        message = input('>>>')
        client.send(message.encode())


5.
import socket

if __name__ == '__main__':
    server = socket.socket()
    server.bind(('10.7.181.117', 8081))
    server.listen(10)

    while True:
        conversation, addr = server.accept()
        print(addr)

        #       
        with open('./luffy2.png', 'rb') as f:
            data = f.read()
        conversation.send(data)


        conversation.close()


6.
import socket

if __name__ == '__main__':
    client = socket.socket()
    client.connect(('10.7.181.117', 8081))

    #     ,        ,        
    image_data = bytes()  #       bytes          
    data = client.recv(1024)
    while data:
        image_data += data
        data = client.recv(1024)

    #        
    with open('./image.png', 'wb') as f:
        f.write(image_data)

    client.close()

7.
"""
   :(python)\java\php  

HTTP(                       )


   :  (js)、iOS      (OC/Swift)、        (java)
"""

"""
python         :    requests
"""
from requests import request

if __name__ == '__main__':
    # https: // www.apiopen.top / satinApi?type = 1 & page = 1
    # GET  :   ?       url    ,   =    ,     &  
    # 1.  url
    url = 'https://www.apiopen.top/satinApi?type=1&page=1'

    # 2.    
    """
    request(    ,    )
       :  
    """
    response = request('GET', url)
    print(type(response), response)

    # 1.            (        )
    text = response.text
    print(type(text), text)

    # 2. json        
    json = response.json()
    print(type(json),json)

    # 3.            
    content = response.content
    print(type(content), content)