zoukankan      html  css  js  c++  java
  • requests

    import requests
    
    import time
    
    url = 'http://www.google.com.hk'
    
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
    try:
        html = requests.get(url, timeout=5).text
        print(html)
        print('success')
    except requests.exceptions.RequestException as e:
        print(111,e)
    
    print(time.strftime('%Y-%m-%d %H:%M:%S'))
    

      

    2019-12-18 23:55:23
    111 HTTPConnectionPool(host='www.google.com.hk', port=80): Max retries exceeded with url: / (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x10c70fc88>, 'Connection to www.google.com.hk timed out. (connect timeout=5)'))
    2019-12-18 23:55:28

    python实现文件下载的方法总结

    https://www.cnblogs.com/jiu0821/p/6275685.html

    from django.db import models


    # Create your models here.
    import os

    def path(instance,file):
    return os.path.join("file",instance.title)
    # return os.path.join("file",instance.title+'_'+file.name)

    class Author(models.Model):
    name = models.CharField(max_length=30)
    age = models.IntegerField()

    class Meta:
    verbose_name = "作者表"
    verbose_name_plural = verbose_name

    def __str__(self):
    return self.name


    class Book(models.Model):
    title = models.CharField(max_length=100)
    price = models.IntegerField()
    authors = models.ManyToManyField(Author)
    code_file = models.FileField(upload_to=path,max_length=256,blank=True,null=True)

    class Meta:
    verbose_name = "图书表"
    verbose_name_plural = verbose_name

    def __str__(self):
    return self.title
    # coding:utf-8
    from django.shortcuts import render, HttpResponse
    from django.http import JsonResponse
    
    # Create your views here.
    
    from .models import Book, Author
    import json
    import requests
    from django.conf import settings
    import os
    from django.core.files.base import ContentFile
    
    def test(request):
        url = r'/Users/just/PycharmProjects/test1/app01/101010.docx'
        obj = Book.objects.filter(title="小红和小明的书").first()
        obj.code_file=r'/Users/just/PycharmProjects/test1/app01/101010.docx'
        obj.save()
    
    
        # url = r'https://www.so.com/?src=so.com'
        # file = requests.get(url)
        # file = file.text
        # path = os.path.join(settings.STATIC_URL,'file',obj.title+"_"+str(obj.id))
        # if not os.path.exists(path):
        #     os.mkdir(path)
        # path = path+r"/test.txt"
        # with open(path,'wb') as f:
        #     f.write(r.content)
        data = {"code_file":obj.code_file.url,"code":1000}
        return JsonResponse(data)
    

      {"code_file": "Users/just/PycharmProjects/test1/app01/101010.docx", "code": 1000}

        obj = Book.objects.filter(id=2).first()
        url = r'https://www.so.com/?src=so.com'
        file = requests.get(url)
        path = os.path.join(settings.BASE_DIR,'file',obj.title)
        if not os.path.exists(path):
            os.makedirs(path)
        path = path+os.sep+"test.txt"
        print(path)
        with open(path,'wb') as f:
            f.write(file.content)
    
        obj.code_file=path
        obj.save()
        data = {"code_file":obj.code_file.url,"code":1000}
        return JsonResponse(data)
    

      {"code_file": "Users/just/PycharmProjects/test1/file/%E5%B0%8F%E7%BA%A2%E5%92%8C%E5%B0%8F%E6%9D%8E%E7%9A%84%E4%B9%A6/test.txt", "code": 1000}

  • 相关阅读:
    git 报错 :Updates were rejected because the tip of your current branch is behind 解决方法
    selenium获取元素的input输入框已经输入的值的三种方法
    chrome(谷歌)浏览器,使用登录,收藏功能
    mybatis学习11-动态SQL语句
    mybatis框架学习-连接池与事务管理
    mybatis学习8-CRUD注解方式
    mybatis学习7-传统dao层开发
    mybatis学习6-使用代理接口的方式开发mybatis的底层实现
    mybatis框架学习-配置文件
    复制,删除 选中单元格对应的行
  • 原文地址:https://www.cnblogs.com/realadmin/p/12064860.html
Copyright © 2011-2022 走看看