如何将字符串表示字节转换回字节?

时间:2022-07-02 20:13:14

I am using SUDS to talk with a web service written by C#. The service recieves a url, crawls its web page, then return its content as byte[].

我正在使用SUDS与C#编写的Web服务进行通信。该服务收到一个URL,抓取其网页,然后将其内容返回为byte []。

its type in SOAP is:

它在SOAP中的类型是:

<s:element minOccurs="0" maxOccurs="1" name="rawByte" type="s:base64Binary" />

sample client codes:

示例客户代码:

>>> from suds.client import Client
>>> url = "http://WSServer/Service1.asmx?wsdl"
>>> client = Client(url)
>>> page = client.service.GetURLContent("http://www.google.co.uk")
>>> print page
(CrawlResult){
   crawStatus = "SUCC"
   rawByte = "PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iY29udGVudC10eXBlIiBjb2 ... "

the problem is how to convert the rawByte from string to bytes, then explain it as text with encoding (like "ascii").

问题是如何将rawByte从字符串转换为字节,然后将其解释为带编码的文本(如“ascii”)。

I am not clear with that.

我不清楚这一点。

Thanks, Daniel.

2 个解决方案

#1


As the SOAP element says, the bytes are base64-encoded.

正如SOAP元素所说,字节是base64编码的。

To decode, use the python module base64.

要解码,请使用python模块base64。

#2


I need to convert it back to string with the binascii module, like:

我需要使用binascii模块将其转换回字符串,如:

>>> btxt = binascii.a2b_base64(page.rawByte)

then btxt can be treated as a normal string.

那么btxt可以被视为普通字符串。

#1


As the SOAP element says, the bytes are base64-encoded.

正如SOAP元素所说,字节是base64编码的。

To decode, use the python module base64.

要解码,请使用python模块base64。

#2


I need to convert it back to string with the binascii module, like:

我需要使用binascii模块将其转换回字符串,如:

>>> btxt = binascii.a2b_base64(page.rawByte)

then btxt can be treated as a normal string.

那么btxt可以被视为普通字符串。