Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Fix: Python requests.get(url) times out but works in browser

If Python requests.get fails with a timeout and the same URL works in the browser then you will need to simulate the browser request as is while invoking requests.get.  
Usually, this will get resolved if you put the "user-agent" header, which tells the server which browser is requesting. 


session = requests.Session()

headers = {
           'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36',          
      }

response = session.request('GET', url, headers=headers, allow_redirects=False)
If this does not resolve the issue then launch the Developer Tools in Chrome and launch the same URL in chrome. Now on Developer Tools check the network tab which shows the requests made by the browser and their details. Copy all the request headers from there and put it into the headers map above and try again.
This time you should not face the issue.

Setup python 2.7 on CentOS 7

Python is most commonly used development and scripting tool. You can setup Python on CentOS by multiple ways.

Setup Python using Yum

This is simplest method to install development tools on CentOS. Run below commands to setup the python using yum.
[vagrant@localhost ~]$ sudo yum -y update [vagrant@localhost ~]$ sudo yum groupinstall -y development
Or
[vagrant@localhost ~]$ sudo yum groupinstall -y 'development tools'

Setup Python using source code

To setup python using source code, you would first download the source code for the version you want to install. Then build the source code and configure.

You can use "wget" command to download the compressed source code. If you don't have wget installed, use yum to install wget as below.
[vagrant@localhost ~]$ sudo yum install wget
You would need "xz" tool to decode the source code.
[vagrant@localhost ~]$ sudo yum install xz-libs
Now you are ready to download the source code. 
Download the source code for python 2.7.6 using below command. You can choose which version you want to install and download sources from here.
[vagrant@localhost ~]$ wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tar.xz
Decode the archive then extract using below command.
[vagrant@localhost ~]$ xz -d Python-2.7.6.tar.xz [vagrant@localhost ~]$ tar -xvf Python-2.7.6.tar
Traverse to extracted python source code directory then run configure.
[vagrant@localhost ~]$ cd Python-2.7.6 [vagrant@localhost ~]$ ./configure --prefix=/usr/local
Run 'make install' build the source and install the binaries. You can also use the 'altinstall' to replace older installation.
[vagrant@localhost ~]$ make altinstall
Once the installation complete, you can verify installation by running the python command. If you get the command not found then update the path variable as below.
[vagrant@localhost ~]$ export PATH="/usr/local/bin:$PATH"
Now your python is setup for use.

Error: No module named SimpleHTTPServer

While running command "python -m SimpleHTTPServer" you may get error "No module named SimpleHTTPServer"

If you are using python 3 and above you may want to execute below command as SimpleHttpServer module is integrated in http.server
"python3 -m http.server"

Else try installing the SimpleHTTPServer module.

Caching is a technique used to store frequently accessed data in a temporary storage layer to improve system performance and reduce latency....