The introduction of the pycurl and StringIO modules when using python3.7 is prone to the above errors
The solution for importing the StringIO module:
StringIO module can only be imported in python2, directly
But python3, STringIO, and cStringIO modules are gone, and to use them you have to import the IO modules:
The solution for importing the StringIO module:
StringIO module can only be imported in python2, directly
from
StringIO
import
StringIO
But python3, STringIO, and cStringIO modules are gone, and to use them you have to import the IO modules:
from io import StringIO
dot_data = StringIO()
StringIO can also be used by importing the Six module:
from six import StringIO
or
from sklearn.externals.six import StringIO
Provide a method that is both Python2 and Python3 compatible:
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
This solves the problem of importing the StringIO module in different Python versions.
But Pycurl is also influenced by the Python version when it encodes strings, as follows:
Write instead
Under python 2, you can use StringIO
object:
import pycurl
from StringIO import StringIO
c = pycurl.Curl()
c.setopt(c.URL,'http://pycurl.io')
buffer = StringIO()
c.setopt(c.WRITEDATA, buffer)
# Same result if using WRITEFUNCTION instead:
#c.setopt(c.WRITEFUNCTION, buffer.write)
c.perform()
# ok
Under python 3, when pycurl USES the bytes
parameter, the response must be written to the BytesIO
object:
import pycurl
from io import BytesIO
c = pycurl.Curl()
c.setopt(c.URL,'http://pycurl.io')
buffer = BytesIO()
c.setopt(c.WRITEDATA, buffer)
# Same result if using WRITEFUNCTION instead:
#c.setopt(c.WRITEFUNCTION, buffer.write)
c.perform()
# ok
Trying to use the StringIO
object will produce an error: :
import pycurl
from io import StringIO
c = pycurl.Curl()
c.setopt(c.URL,'http://pycurl.io')
buffer = StringIO()
c.setopt(c.WRITEDATA, buffer)
c.perform()
The error message is as follows:
The following idiom is available for code that requires compatibility with Python2 and Python3:
import pycurl
try:
# Python 3
from io import BytesIO
except ImportError:
# Python 2
from StringIO import StringIO as BytesIO
c = pycurl.Curl()
c.setopt(c.URL,'http://pycurl.io')
buffer = BytesIO()
c.setopt(c.WRITEDATA, buffer)
c.perform()
# ok
# Decode the response body:
string_body = buffer.getvalue().decode('utf-8')
div>
Read More:
- curl: (23)failed writing body(0!=3810)
- Mac install pycurl error: python setup.py egg_info" failed with error code 1”
- Macro generated_ UCLASS_ Body () and generated_ Analysis of body ()
- Curl returns empty reply from server. Due to the processing of special characters, curl cannot be accessed and the browser can access it.
- XML tag has empty body less… (Ctrl+F1) Reports empty tag body. The validation works in XML / JSP
- RCurl error-fatal error: curl/curl.h: No such file or directory
- TypeError: Failed to execute ‘fetch‘ on ‘Window‘: Request with GET/HEAD method cannot have body.
- On curl: (2) failed initialization
- Visual SVN server provider failed to perform the attempted operation 0x80041024 after win7 upgraded win10
- Cannot find module ‘body-parser’
- curl: (25) Failed FTP upload: 550 Solution
- Required request body is missing with ContentCachingRequestWrapper
- error: RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 8)
- curl: (7) Failed connect to localhost:9200; Connection refused
- curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
- Girl: RPC failed; HTTP 411 curl 22 the requested URL returned error: 411 length required:
- [Linux] curl: (7) failed to connect to 127.0.0.1 port 1086: connection reused solution
- Error: required request body is missing, @ requestbody annotation usage
- Error: RPC failed; HTTP 504 curl 22 the requested URL returned error: 504 gateway time out
- Error: unable to perform an operation on node ‘rabbit@rabbitma‘ please see diamostics infoxmation