美文网首页
2018-09-12-作业

2018-09-12-作业

作者: fanzl | 来源:发表于2018-09-12 20:04 被阅读0次
    1. 客户端和服务器聊天,可以一直聊天,直到一方发送’拜拜’。然后就可以和下一个人一直聊
      服务器
    import socket
    server=socket.socket()
    server.bind(('10.7.153.133',6666))
    
    server.listen(512)
    while True:
        connect, addr = server.accept()
        print(addr)
        while True:
    
            recv_data=connect.recv(1024)
            print('客户端说:'+str(recv_data,encoding='utf-8'))
            if str(recv_data,encoding='utf-8')=='拜拜':
                connect.close()
                break
            message=input('服务器发送:')
            if message=='拜拜':
                connect.close()
                break
            connect.send(bytes(message,encoding='utf-8'))
    

    客户端

    import socket
    
    client=socket.socket()
    client.connect(('10.7.153.133',6666))
    
    while True:
    
        message=input('客户端发送:')
    
        client.send(message.encode())
    
        recv_data=client.recv(1024)
        print('从服务器接收:'+str(recv_data,encoding='utf-8'))
    client.close()
    

    2,2. 下载网络图片(

    https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg)到本地

    import requests
    
    url='https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2808438283,4249462766&fm=26&gp=0.jpg'
    
    response=requests.get(url)
    print(response)
    content=response.content
    with open('./files/tupian.jpg','wb') as f:
        f.write(content)
    
    tupian.jpg

    相关文章

      网友评论

          本文标题:2018-09-12-作业

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