socket练习

作者: GHope | 来源:发表于2018-08-08 19:32 被阅读58次

    服务器端

    import socket
    import requests
    import re
    
    if __name__ == '__main__':
        server = socket.socket()
        server.bind(('*.*.*.*', 8080))
        server.listen(512)
    
        while True:
            cover, addr = server.accept()
    
            while True:
                message = input('我:')
                cover.send(message.encode())
    
                message_re = cover.recv(1024).decode(encoding='utf-8')
                # 1.如果是'拜拜'
                if message_re == '拜拜':
                    cover.close()
                    break
                # http://122.2323.232.23/
                elif re.fullmatch(r'http://(\w+\.){2}(\w+)/[\w?=&%@/\.]+ ', message_re):
                    print('是网址')
                    match = re.search(r'(\.png|\.jpg|\.gif|\.jpeg|\.ico) $', message_re)
    
                    # 如果是图片
                    if match:
                        # 返回的是服务器的响应
                        response = requests.request('GET',message_re)
                        # 获取服务器返回的响应头
                        print(response.headers)
                        # 获取响应体的二进制
                        content = response.content
                        with open('./image'+match.group(), 'wb') as f:
                            f.write(content)
                    else:
                        response = requests.request('GET', message_re)
                        txt = response.text
                        with open('./text', 'a', encoding='utf-8'):
                            f.write(txt)
    
                else:
                    print(message_re)
    
    

    客户端

    import socket
    
    if __name__ == '__main__':
        client = socket.socket()
        client.connect(('*.*.*.*', 8080))
        while True:
            print(client.recv(1024).decode(encoding='utf-8'))
            message = input('>>>')
            client.send(message.encode())
    
    

    相关文章

      网友评论

        本文标题:socket练习

        本文链接:https://www.haomeiwen.com/subject/adaavftx.html