Issue with python https server script

Hi,

Following is our python script

import ssl
import socket
import threading
import os
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from SocketServer import ThreadingMixIn

#change the current working directory to specified path
os.chdir('/')

class MyHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == '/ip':
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.wfile.write('Your IP address is %s' % self.client_address[0])
return
else:
return SimpleHTTPRequestHandler.do_GET(self)

class HTTPServerV6(HTTPServer):
address_family = socket.AF_INET6

class ThreadedHTTPServer(ThreadingMixIn, HTTPServerV6):
"""Handle requests in a separate thread."""

def main():
server = ThreadedHTTPServer(('::', 8080), MyHandler)
server.socket = ssl.wrap_socket (server.socket,
keyfile="/opt/gNB/cfg/client.key",
certfile='/opt/gNB/cfg/client.crt',
ca_certs='/opt/gNB/cfg/ca.pem', server_side=True, cert_reqs=ssl.CERT_REQUIRED)
server.serve_forever()

if name == 'main':
main()

Seeing following crash
Traceback (most recent call last):
File “httpsServer.py”, line 5, in
from BaseHTTPServer import HTTPServer
File “/usr/lib64/python2.7/BaseHTTPServer.py”, line 81, in
import mimetools
File “/usr/lib64/python2.7/mimetools.py”, line 6, in
import tempfile
2022-06-14:03-13-05:098633649 SelfMonitoring[60972]: Config: MONITORING_REBOOT_IND_LOCAL_FILE = “/tmp/rebootrequired.txt”
File “/usr/lib64/python2.7/tempfile.py”, line 35, in
from random import Random as _Random
File “/usr/lib64/python2.7/random.py”, line 879, in
_inst = Random()
File “/usr/lib64/python2.7/random.py”, line 97, in init
self.seed(x)
File “/usr/lib64/python2.7/random.py”, line 111, in seed
a = long(_hexlify(_urandom(16)), 16)**
OSError: [Errno 1] Operation not permitted**

Python version :- Python 2.7.5
Need help why that crash is happening.

Hello, @shikha-jindal . Welcome to Python!

When one has a crash with a traceback, it is often helpful to see what was the line of one’s own code which was running when the crash happened.

Traceback (most recent call last):
File “httpsServer.py”, line 5, in
from BaseHTTPServer import HTTPServer
...[rest elided]...

This looks like that line, from your program:

Can you run the Python interpreter interactively in your program’s source directory? Try typing the lines of your program by hand, up to and including this line. Does this line run successfully, or does it crash?

I am expecting that this line will crash. Then that takes you to the next step: finding out whay this import fails. But if this line does not crash, the investigation goes in a different direction.

Jim has shown where you can find the failing line in your code.

In this case the actual exception at the end of the traceback may lead us directly to the culprit of the problem:

  • OSError: [Errno 1] Operation not permitted - Operating system error, saying that you do not have the access rights to access a resource (usually a file)
  • File “/usr/lib64/python2.7/random.py”, line 111, in seed - function seeding the Python’s pseudorandom number generator
  • _urandom(16) - I would guess this function needs to read (16 bytes) from /dev/urandom

I would check if your user can read from the device file(s). This is how it looks like on my system (the sequence values should be different every time):

$ od -tx1 -N16 /dev/urandom
0000000 c9 f4 dc 85 e6 cd 35 05 34 24 ed 59 f9 62 31 76
0000020
$ od -tx1 -N16 /dev/random
0000000 75 df b8 30 17 54 f8 6e 99 70 ca aa 4b b0 55 a8
0000020

Access rights on my system (everyone can read):

$ ls -l /dev/*random
crw-rw-rw- 1 root root 1, 8 Jun 26 20:51 /dev/random
crw-rw-rw- 1 root root 1, 9 Jun 26 20:51 /dev/urandom

Your Python version is almost 10 years old. Python 2 is out of support for more than two years. You should not use such an old version. Maybe newer version would show you a better error message :slight_smile:

Actually this is not happening every time.
Script working fine. only once we observed this issue. We tried to reproduce also but couldn’t.
So not sure what is causing this issue.