小编bud*_*dge的帖子

Python - BaseHTTPServer do_GET() - wfile.write(filedata) 管道损坏

我需要设置一个 Python Web 服务器,它返回几个 3MB 文件。它使用 baseHTTPServer 来处理 GET 请求。如何使用 wfile.write() 发送 3MB 文件?

from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import BaseHTTPServer


class StoreHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    request_queue_size = 100

    def do_GET(self):
        try:

            filepath = os.path.join(os.path.join(os.path.dirname(__file__), "tools"), "tools.zip")
            if not os.path.exists:
                print 'Tool doesnt exist'

            f = open(filepath, 'rb')
            file_data = f.read()
            f.close()

            self.send_header("Content-type", "application/octet-stream")
            self.end_headers()
            self.wfile.write(file_data) 
            self.send_response(200)
        except Exception,e:
            print e
            self.send_response(400)
Run Code Online (Sandbox Code Playgroud)

错误:

----------------------------------------
Exception happened during processing of request from ('192.168.0.6', 41025)
Traceback (most recent call last):
  File …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
1
解决办法
5085
查看次数

速率限制功能 40/second with "golang.org/x/time/rate"

我正在尝试使用“golang.org/x/time/rate”来构建一个函数,该函数在令牌可用之前一直阻塞。这是使用库将代码块速率限制为每秒 40 个请求,存储桶大小为 2 的正确方法吗?

type Client struct {
    limiter        *rate.Limiter
    ctx context.Context
}

func NewClient() *Client {
    c :=Client{}
    c.limiter = rate.NewLimiter(40, 2)
    c.ctx = context.Background()
    return &c
}

func (client *Client) RateLimitFunc() {

    err := client.limiter.Wait(client.ctx)
    if err != nil {
        fmt.Printf("rate limit error: %v", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

对我调用的代码块进行速率限制

RateLimitFunc()
Run Code Online (Sandbox Code Playgroud)

我不想使用自动收报机,因为我希望速率限制器考虑调用代码运行的时间长度。

go

5
推荐指数
1
解决办法
3860
查看次数

如何复制结构并取消引用所有指针

如何将 Item 结构和所有指针复制到新结构?

type Item struct {
    A    []*ASet     `json:"a,omitempty"`
    B    []*BSet.    `json:"b,omitempty"`
    C    []*CSet.    `json:"c,omitempty"`
}


type ASet struct {
    UID   string     `json:"uid,omitempty"`
    Items []*ItemA `json:"member,omitempty"`
}

type ItemA struct {
    UID  string `json:"uid,omitempty"`
    Portset []*PortSet `json:"portset,omitempty"`
}

type PortSet struct {
    UID   string     `json:"uid,omitempty"`
    Ports []*Port `json:"member,omitempty"`
}

type Port struct {
    UID  string `json:"uid,omitempty"`
    Port int    `json:"port,omitempty"`
}
Run Code Online (Sandbox Code Playgroud)

我不希望新结构引用旧结构。

struct pointers copy go dereference

3
推荐指数
1
解决办法
6956
查看次数

在 Go 中使用位掩码(使用 iota 进行 Go 枚举)

有点卡住了。

我正在尝试弄清楚如何使用 go 枚举从 const 获取位掩码值。例如,如果密钥为 5,即 0101 位,则将是狗和鱼。获取位值(1、2、4、8、16、32、64 等)以便我可以选择字符串值并返回动物集的最简单方法是什么?

type Key int

const (
    Dog Key = 1 << iota
    Cat
    Fish
    Horse
    Snake
    Rabbit
    Lion
    Rino
    Hedgehog
)
Run Code Online (Sandbox Code Playgroud)

一直在阅读,但我无法解决这个问题。

go

3
推荐指数
1
解决办法
3636
查看次数

Golang Filepath.Walk在大型目录上出现恐慌错误

我想通过C盘,我阅读上的所有文件走路可以在围棋是低效的,但我不能工作了这样做的时候,我告诉步行函数返回一个零错误代码,为什么我总是收到一个严重错误。

package files

import (
   "path/filepath"
   "os"
   "fmt"
 )

func walkpath(path string, f os.FileInfo, err error) error {
    fmt.Printf("%s with %d bytes\n", path,f.Size())
    return nil
}

func GetFiles() {
    err := filepath.Walk("C:\\", walkpath)
    if err != nil {
        fmt.Printf(err.Error())
    }
}
Run Code Online (Sandbox Code Playgroud)

紧急错误:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x40 pc=0x46d0d6]

goroutine 1 [running]:
files.walkpath(0xc0820a8f80, 0xf, 0x0, 0x0, 0x664270, 0xc082408840, 0x0, 0x0)
        C:/project/src/files/files.go:11 +0x66
path/filepath.walk(0x529140, 0x3, 0x6641e8, 0xc082012240, 0x560d50, 0x0, 0x0)
        c:/go/src/path/filepath/path.go:370 …
Run Code Online (Sandbox Code Playgroud)

go panic

2
推荐指数
1
解决办法
1442
查看次数

标签 统计

go ×4

copy ×1

dereference ×1

panic ×1

pointers ×1

python ×1

struct ×1