debugging python web service - python

I am using the instructions found here, to try to inspect the HTTP commands being sent to my webserver.
However, I am not seeing the HTTP commands being printed on the console as suggested in the tutorial. Does anyone know how to display/debug the HTTP commands at the CLI?
I am running Python 2.6.5 on Linux Ubuntu

The tutorial information seems to be deprecated.
Correct way to debug with urllib2 nowadays is:
import urllib2
request = urllib2.Request('http://diveintomark.org/xml/atom.xml')
opener = urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
feeddata = opener.open(request).read()
Debugging with urllib works the old way though.

Related

how to force python call to speedtest.py use secure servers

I'm calling speedtest.py from my python program in order to run a speed test.
After importing speedtest.py I create a speedtest object using
s = speedtest.Speedtest()
servers = s.get_best_server()
then run upload/download tests.
This has been working, but recently it has been failing.
I found if I just ran speedtest from the cli, I'd get a 403 forbidden error.
I then read than Speedtest now requires the use of secure servers.
SO, if from the cli I type
speedtest --secure
it runs perfectly.
My question is:
How can I tell speedtest to use secure servers from my python program?
I've tried variations of
s = speedtest.Speedtest("secure")
and
servers = s.get_best_server("secure")
I haven't used the Python API for speedtest-cli, but looking at the source, Speedtest() has a secure parameter which presumably makes it require secure servers. So you'd just need to specify it when instantiating the class:
s = speedtest.Speedtest(secure=True)
BTW, there is documentation for the Python API, but it's super barebones and doesn't mention this parameter at all.

I get an error xxx has been blocked by CORS policy in React 2

At this CORS issue
I asked the client to edit the backend side.
Due to my lack of knowledge
I do not understand the conversation of the following.
My client : Can you implement backend to overcome this cors issue.
Me : Who made Backend and what language? My client : I meant,
backend for your frontend. Not actual backend , frontend backend that
will act as a proxy. You can implement this backend proxy in python
It means that Python can act as a bridge between Backend and React?
(Not sure if it's possible...)
To solve the CORS issue on the front side
Write the code below
axios.defaults.headers.get['Access-Control-Allow-Origin'] = '*';
Also, just in case, I used a browser plug-in to allow access.
Still getting the same error. .
I did try it Postman.
Launch a browser that can ignore CORS with the following command in the terminal
By accessing the URL from there
Once I was able to work around the CORS issue.
open -n -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --user-data-dir="/tmp/chrome_dev_test" --disable-web-security

Python 3.5 urllib won't open webpage in browser

I tried following code in VS2015, Eclipse and Spyder:
import urllib.request
with urllib.request.urlopen('https://www.python.org/') as response:
html = response.read()
In call cases it won't open the webpage in the browser. I am not sure what is the problem. Debug won't help. In VS2015 the program exists with code 0 which I suppose means successful.
You are using wrong library for the job. urllib module provides functions to send http requests and capture the result in your program. It has nothing to do with a web browser. What you are looking for is the webbrowser module. Here is an example:
import webbrowser
webbrowser.open('http://google.com')
This will show the web page in your browser.
urllib
is a module that is used to send request to web pages and read its contents.
Where as:
webbrowser
is used to open the desired url.
It can used as follows:
import webbrowser
webbrowser.open('http://docs.python.org/lib/module-webbrowser.html')
which usually re-uses existing browser window.
To open in new window:
webbrowser.open_new('http://docs.python.org/lib/module-webbrowser.html')
To open in new tab:
webbrowser.open_new_tab('http://docs.python.org/lib/module-webbrowser.html')
To access via command line interface:
$ python -m webbrowser -t "http://www.python.org"
-n: open new window
-t: open new tab
Here is python documentation for webbrowser:
python 3.6
python 2.7

Python "app transport security" error under El Capitan

Some of my Python shell scripts are newly throwing security errors under Apple OSX 10.11, El Capitan. It seems the new App Transport Security doesn't like how the scripts are calling HTTP resources in plain text, rather than using HTTPS.
Fetching http://blahblah.com
Python[5553:5648168] App Transport Security has blocked a cleartext HTTP (http://)
resource load since it is insecure. Temporary exceptions can be configured
via your app's Info.plist file.
How might I go about fixing this? There is no HTTPS resource I can call, so I'm stuck with HTTP. The advice from Apple is to make an exception in the app's info.plist file, but this is a Python script invoked from a shell script, so there is no info.plist file to be edited.
Ideas? The root problem seems to be with webkit2png, which is in Python. Its non-HTTPS requests are being blocked by ATS, and there is no info.plist to modify.
I found a solution here that worked for me: https://apple.stackexchange.com/questions/210588/how-does-one-configure-a-temporary-exception-to-ats-on-el-capitan-and-fix-webkit
First make sure you have a version of webkit2png that is new enough to have the --ignore-ssl-check option. Version 0.5 does NOT have this option.
Second, you need to edit the source file and add a couple lines of code as shown here: https://github.com/bendalton/webkit2png/commit/9a96ac8977c386a84edb674ca1518e90452cee88
Finally use option as indicated in the solution linked above (copied here for convenience):
webkit2png --ignore-ssl-check [options] [http://example/]
Thanks for Arthur Hebert
At first I'm confused for the code then I figure it out
so I summarise the steps as follows for reference
import AppKit
Add the following code in your py script
AppKit.NSBundle.mainBundle().infoDictionary()['NSAppTransportSecurity'] = dict(NSAllowsArbitraryLoads = True)
Assume you are sure to use no HTTPS resource in MAC OSX higher than 10.11
The installation of webkit2png is not necessary in my case

Websocket library for python

Hi I need a websocket server in python which supports the protocol used in chrome 16(protocol version 13). Tornado and twisted are not working. Websockify works but i can't find any documentation for it. I need minimal setup means lesser imports. Please help me out here thanks in advance.
Maybe you could take a look to pywebsocket, it claims to support protocol version 13 and is designed for :
The pywebsocket project aims to provide a WebSocket standalone server
and a WebSocket extension for Apache HTTP Server, mod_pywebsocket.
Autobahn is another implementation of websockets :
Autobahn WebSockets for Python provides an implementation of the
WebSockets protocol which can be used to build WebSockets clients and
servers
ws4py : Websocket for python :
Python library providing support for the WebSocket protocol defined in
RFC 6455
Here are some examples of implementing a websocket server in Python. Be sure to read and apply the comments on the code of the following examples, because there may be some bugs:
http://popdevelop.com/2010/03/a-minimal-python-websocket-server/ : It has been tested on Chrome, according to the author of the code.
http://mumrah.net/websockets-in-python : At the end of this blog page, the author has included the URL to a Python implementation of a websocket server.
http://dev.enekoalonso.com/2010/05/22/more-websockets-now-with-python/: only works on Chrome, according to the author.
This page contains an implementation of a Python websocket server that can be used through imports:
https://github.com/AdrianGaudebert/python-websocket-server
You should know that the license for using this is MIT. It may only work with Python 3.0.
WebSocket Echo example works on Chrome/16.0.912.63.
It uses txWS a simple library for adding WebSockets server support to your favorite Twisted applications.
If you are still interested in using websockify, there is a simple example of using it to build an echo server included](https://github.com/kanaka/websockify/blob/master/tests/echo.py).
You can run it like this (from a websockify checkout):
./tests/echo.py 8080
The browse to localhost:8080/tests/echo.html. Enter localhost, 8080 for the WebSocket host and port and hit connect. You should see the client sending messages and the server echoing them back (with a "You said: " prefix).

Categories