第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

直接從 Golang 打印到外部打印機(jī)

直接從 Golang 打印到外部打印機(jī)

Go
慕尼黑5688855 2022-10-24 15:46:43
我可以從 Golang 直接打印到我的(物理的、外部的)打印機(jī),而不使用打印機(jī)驅(qū)動(dòng)程序或 CUPS 或任何其他此類復(fù)雜性嗎?
查看完整描述

1 回答

?
白衣染霜花

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊

是的!下面使用 IPP(互聯(lián)網(wǎng)打印協(xié)議)從 Golang 打印 postscript 文件,過去 20 年制造的大多數(shù)網(wǎng)絡(luò)打印機(jī)都支持該協(xié)議:


import (

    "bytes"

    "fmt"

    "io"

    "io/ioutil"

    "net/http"

    "os"

    "strconv"


    "github.com/alexflint/go-arg"

    "github.com/kr/pretty"

    "github.com/phin1x/go-ipp"

)


func Main() error {

    var args struct {

        URI            string `arg:"positional,required"`

        PostscriptFile string `arg:"positional,required"`

    }

    arg.MustParse(&args)


    // define a ipp request

    req := ipp.NewRequest(ipp.OperationPrintJob, 1)

    req.OperationAttributes[ipp.AttributeCharset] = "utf-8"

    req.OperationAttributes[ipp.AttributeNaturalLanguage] = "en"

    req.OperationAttributes[ipp.AttributePrinterURI] = args.URI

    req.OperationAttributes[ipp.AttributeRequestingUserName] = "some-user"

    req.OperationAttributes[ipp.AttributeDocumentFormat] = "application/octet-stream"


    // encode request to bytes

    payload, err := req.Encode()

    if err != nil {

        return fmt.Errorf("error encoding ipp request: %w", err)

    }


    // read the test page

    postscript, err := ioutil.ReadFile(args.PostscriptFile)

    if err != nil {

        return fmt.Errorf("error reading postscript file: %w", err)

    }

    payload = append(payload, postscript...)


    // send ipp request to remote server via http

    httpReq, err := http.NewRequest("POST", args.URI, bytes.NewReader(payload))

    if err != nil {

        return fmt.Errorf("error creating http request: %w", err)

    }


    // set ipp headers

    httpReq.Header.Set("Content-Length", strconv.Itoa(len(payload)))

    httpReq.Header.Set("Content-Type", ipp.ContentTypeIPP)


    // perform the request

    var httpClient http.Client

    httpResp, err := httpClient.Do(httpReq)

    if err != nil {

        return fmt.Errorf("error executing http request: %w", err)

    }

    defer httpResp.Body.Close()


    // read the response

    buf, err := io.ReadAll(httpResp.Body)

    if err != nil {

        return fmt.Errorf("error reading response body: %w", err)

    }


    // response must be 200 for a successful operation

    // other possible http codes are:

    // - 500 -> server error

    // - 426 -> sever requests a encrypted connection

    // - 401 -> forbidden -> need authorization header or user is not permitted

    if httpResp.StatusCode != 200 {

        return fmt.Errorf("printer said %d: %s", httpResp.StatusCode, buf)

    }


    // decode ipp response

    resp, err := ipp.NewResponseDecoder(bytes.NewReader(buf)).Decode(nil)

    if err != nil {

        return fmt.Errorf("error decoding ipp response: %w", err)

    }


    // print the response

    fmt.Println("Submitted print job. Response was:")

    pretty.Println(resp)

    return nil

}

使用的 URL 只是 http://ip-address-of-myprinter(這是在兄弟 HL 系列打印機(jī)上測試的)


查看完整回答
反對 回復(fù) 2022-10-24
  • 1 回答
  • 0 關(guān)注
  • 1416 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號