Using Django in Docker unable to fetch rest api - django

I have built the webapp using Django and also configured for Rest api. I am able to fetch the data via curl and also by python script. Issue I'm running into, when using Docker container, when user fill in the data and submit, it goes thru and ran the python function, but it stop working at when fetching the data via api and thru errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.6/site-packages/requests/models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I'm able to curl the URL inside the container. I get the same error when shell into the container in python3.6
sh-4.2# python3.6
Python 3.6.8 (default, May 2 2019, 20:40:44)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import requests, json
>>> url = 'http://127.0.0.1:8001/db/data/'
>>> response = requests.get(url)
>>> parsedjson = json.loads(response.text)
ERROR SAME AS ABOVE . . .
parsedjson = response.json()
ERROR SAME AS ABOVE . . .
>>> print(response.status_code)
503
>>> exit()
sh-4.2# curl http://127.0.0.1:8001/db/data/
[{"center":{"name":"ABC"},"main":{"code":{"name":"ABC"},"subnet":"10.10.12.0",...."}]
sh-4.2#
any idea? how to fix it?
thank you for the help in advance..

Related

CERTIFICATE_VERIFY_FAILED in urllib2 but not requests, on Python 2.7.18

My longstanding Python 2.7.17 environment got corrupted, and so I installed Python 2.7.18 using the macOS 64-bit installer downloadable from python.org. Now I get errors when loading HTTPS sites using urllib2 but not requests.
Here is the environment, running macOS Mojave:
Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 19 2020, 20:48:48)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Works fine with requests:
>>> import requests
>>> url='https://github.com'
>>> requests.get(url)
<Response [200]>
Fails with urllib2:
>>> import urllib2
>>> urllib2.urlopen(url)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 429, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 447, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1241, in https_open
context=self._context)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:727)>
This occurs in both the plain 2.7.18 I installed and in virtual environments created from it. The problem is bigger than just urllib2, as certain database modules fail, too, that do not use urllib2. Moving to Python3 is not currently an option.
I tried pip install --upgrade certifi but that did not help.
Per [1] monkey-patching fixes the problem, but that is not a viable solution because this is destined to be production code.
Per [1] explicitly setting a non-verifying SSL context sometimes works, but again, it is not viable, plus it fails in certain third-party database interface code.
The answer may be in PEPP [2] or PEPP 493 [3], but the sun will swallow the earth before I understand those.
[1] urllib and "SSL: CERTIFICATE_VERIFY_FAILED" Error
[2] https://www.python.org/dev/peps/pep-0476/
[3] https://www.python.org/dev/peps/pep-0493/

S3 Select with boto3 - internalerror

Has anyone got "S3 Select" (https://aws.amazon.com/blogs/aws/s3-glacier-select/ ,
https://aws.amazon.com/about-aws/whats-new/2018/04/amazon-s3-select-is-now-generally-available/) with boto3 (or even cli or another sdk) working? I am getting cryptic InternalError below:
Running this on EC2 that has an IAM role:
[ec2-user#ip-blah bin]$ ./python
Python 2.7.13 (default, Jan 31 2018, 00:17:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import boto3
>>> s3 = boto3.client('s3')
>>> r = s3.select_object_content(
... Bucket='mybucketname',
... Key='mypath/file.txt',
... ExpressionType='SQL',
... Expression="select count(*) from s3object s",
... InputSerialization = {'CSV': {"FileHeaderInfo": "Use"}},
... OutputSerialization = {'CSV': {}},
... )
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/home/ec2-user/venv/local/lib/python2.7/site-packages/botocore/client.py", line 314, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/home/ec2-user/venv/local/lib/python2.7/site-packages/botocore/client.py", line 612, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (InternalError) when calling the SelectObjectContent operation (reached max retries: 4): We encountered an internal error. Please try again.
My guesses:
check permissions on s3
adapt 'RecordDelimiter','FieldDelimiter', 'QuoteCharacter' if neccessary on InputSerialization
check structure on csv file (number of headers matches data columns, escaping spec. chars, whitespaces, /n as new line def.,.)
try
...
Expression="SELECT * FROM S3Object s", InputSerialization={'CSV': {}}, OutputSerialization={'CSV': {}}, ...
hope that helps a little!

python: socketio-client not working

I have written this python code that uses socketio client module.
from socketIO_client import SocketIO
print "connecting to server"
socketIO = SocketIO('localhost', 8888, transports=['websocket'])
print "Connected"
def sendSocketId():
socketIO.emit('authenticate_python', "Python is connected")
def socketDisconnect():
socketIO.disconnect()
def doSomething(data):
print "message from ui : : ", data
socketIO.emit("msg_from_python","Message from python : : Hi! " + data)
try:
print socketIO.connected
if socketIO.connected:
sendSocketId()
socketIO.on('msg_from_node', doSomething)
socketIO.wait()
except Exception as e:
print "Exception : : ", e
socketDisconnect()
But when I connect to the server and run this file, it raises the following error:
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
connecting to server
Traceback (most recent call last):
File "C:\Users\student\app.py", line 4, in <module>
socketIO = SocketIO('localhost', 8888, transports=['websocket'])
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 353, in __init__
resource, hurry_interval_in_seconds, **kw)
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 54, in __init__
self._transport
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 62, in _transport
self._engineIO_session = self._get_engineIO_session()
File "C:\Python27\lib\site-packages\socketIO_client\__init__.py", line 76, in _get_engineIO_session
transport.recv_packet())
StopIteration
>>>
I have no clue about this error and also there no no proper documentations as to how should we use this socketio client module.
The issue was resolved by uninstalling socket.io 2.0.0 version and installing watever version of it matches with the socketIO-client version.
This module socketIO-client-nexus fixes issue with compatible socket version socket.io protocol 1.x
https://pypi.org/project/socketIO-client-nexus/0.7.6/
which is not backwards compatible,
if you want to communicate using socket.io protocol 0.9 please use soketIO-client 0.5.7.2
https://pypi.org/project/socketIO-client/0.5.7.2/
Don't forget to change from import socketIO-client to socketIO-client-nexus

Invalid token error in Telegram Bot API

I'm new to telegram bot api.
I installed telegrom package 📦 and started to run my first code.
but I cannot run my first code !
Can anyone know why this is happening? what should I do ?
Thank you 🙏
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 12:39:47)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import telegram
>>> bot = telegram.Bot(token='TOKEN')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/telegram/bot.py", line 53, in __init__
self.token = self._validate_token(token)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/telegram/bot.py", line 79, in _validate_token
raise InvalidToken()
telegram.error.InvalidToken: Invalid token
>>>
For your line
bot = telegram.Bot(token='TOKEN')
You need to replace 'TOKEN' with your bot's token, as the string 'TOKEN' is just a placeholder.
You may generate a bot token by talking to BotFather and creating a new bot.

Python 2.7 & wget error 17 "No usable temporary file name found"

I am just learning python 2.7 and trying to use wget to download a file from a URL.
I get the following error (and here are the exact input & syntax):
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import wget
>>> wget.download('http://www.csun.edu/~rd436460/Labview/Lecture-Overview.pdf')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\wget.py", line 506, in download
(fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=prefix, dir=".")
File "C:\Python27\lib\tempfile.py", line 314, in mkstemp
return _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Python27\lib\tempfile.py", line 257, in _mkstemp_inner
raise IOError, (_errno.EEXIST, "No usable temporary file name found")
IOError: [Errno 17] No usable temporary file name found
>>>
My question is....... What is the correct syntax to get this file to download without error?
I am running Python 2.7 On Windows 10...... Also I saw some of the other post of temporary file errors due to no drive space. On my system I have plenty of drive space available.