码迷,mamicode.com
首页 > 其他好文 > 详细

requests

时间:2019-12-19 09:36:56      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:cts   objects   图书   exce   har   tpc   ons   ret   auth   

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

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}

requests

标签:cts   objects   图书   exce   har   tpc   ons   ret   auth   

原文地址:https://www.cnblogs.com/realadmin/p/12064860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!