Python: Accessing a web page with basic authentication

Here I’m demonstrating on how to access a web page with basic authentication in PYTHON code. This is very simple python code example using urllib2 module and no third party modules being used. 

Using this code you can access any web link from your code and get return data from that link. This is like web proxy call 


 

import urllib2

accessURL = ‘https://remoteserver.com/accesspage’
pwdmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwdmanager.add_password(None, accessURL, “username”, “password”)
authhandler = urllib2.HTTPBasicAuthHandler(pwdmanager)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
returnPage = urllib2.urlopen(accessURL).read()

print returnPage


You can also send data to that URL call and get return data if that link returns. 


 

import urllib2

accessURL = ‘https://remoteserver.com/accesspage’
pwdmanager = urllib2.HTTPPasswordMgrWithDefaultRealm()
pwdmanager.add_password(None, accessURL, “username”, “password”)
authhandler = urllib2.HTTPBasicAuthHandler(pwdmanager)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
requestdata = urllib.urlencode({‘FIRST_NAME’ : ‘Sumant’,’LAST_NAME’ : ‘Kuchipudi’})
responsedata = urllib2.urlopen(accessURL, data=requestdata).read()

print responsedata


 

I hope this helps you

Title Menu bar using CSS and Hover

This post is basically showing how the each Menu title populates sub menu using hover functionality in easiest way. This is designed based on CSS styling and uses HTML table header. I tried in google to find this but I couldn’t and eventually I’ve developed it so that people like me will no longer suffer to find kind of examples.

See the following code snippets.

.header,.header ul  {
padding: 0;
margin: 1;
list-style: none;
width: 150px;
}
.header a {
font-weight: bold;
font-family: Times New Roman;
font-style: normal;
font-size: 15px;
color: black;
text-decoration: none;
display: block;
padding: 0px 0px 0px 0px;
margin: 1;
}
.header div {
display: inline;
width: 150px;
}
.header li {
float: center;
padding: 0px 8px 2px 0px;
width: 180px;
}
/* all hoover list items */
.header li ul {
position: absolute;
background: #A0CFEC;
padding: 0px 0px 0px 0px;
width: 150px;
left: -9999px;
border-style:solid;
border-color:black;
border-width:1px;
}
/* second-level lists */
.header li:hover ul {
left: auto;
}
/* nest list under hovered list items */
.header li ul li a {
color: black;
text-decoration: none;
display: block;
font-family: Times New Roman;
font-weight: 900;
font-size: 0.7em;
width:149px;
}
.header li ul li a:hover {
color: black;
background:#A0CFEC;
width=180px;
background: #90BAD4;
}
.down {
color: #808080;
font-family:arial;
font-size: 0.8em;
font-weight: bold;
}
.uid_spacing {
padding: 0px 95px 0px 0px;
}
.nid_spacing {
padding: 0px 80px 0px 0px;
}
.fn_spacing {
padding: 0px 80px 0px 0px;
}
.ln_spacing {
padding: 0px 60px 0px 0px;
}

I hope the above CSS code will help you to get the idea. If you want to test the page, just copy the following code and run it from your browser.

<HTML>
<HEAD>
<TITLE> HTML Hover functionality </TITLE>
<STYLE TYPE=”text/css”>
.header,.header ul  {
padding: 0;
margin: 1;
list-style: none;
width: 150px;
}
.header a {
font-weight: bold;
font-family: Times New Roman;
font-style: normal;
font-size: 15px;
color: black;
text-decoration: none;
display: block;
padding: 0px 0px 0px 0px;
margin: 1;
}
.header div {
display: inline;
width: 150px;
}
.header li {
float: center;
padding: 0px 8px 2px 0px;
width: 180px;
}
/* all hoover list items */
.header li ul {
position: absolute;
background: #A0CFEC;
padding: 0px 0px 0px 0px;
width: 150px;
left: -9999px;
border-style:solid;
border-color:black;
border-width:1px;
}
/* second-level lists */
.header li:hover ul {
left: auto;
}
/* nest list under hovered list items */
.header li ul li a {
color: black;
text-decoration: none;
display: block;
font-family: Times New Roman;
font-weight: 900;
font-size: 0.7em;
width:149px;
}
.header li ul li a:hover {
color: black;
background:#A0CFEC;
width=180px;
background: #90BAD4;
}
.down {
color: #808080;
font-family:arial;
font-size: 0.8em;
font-weight: bold;
}
.uid_spacing {
padding: 0px 95px 0px 0px;
}
.nid_spacing {
padding: 0px 80px 0px 0px;
}
.fn_spacing {
padding: 0px 80px 0px 0px;
}
.ln_spacing {
padding: 0px 60px 0px 0px;
}
</STYLE>

</HEAD>
<BODY>
<H3>HTML table header (like CSS menu bar) with hover functioanlity</H3>
<FORM ACTION=”” METHOD=”POST” NAME=”searchForm”>

<BR /></BR />
<TABLE WIDTH=”600″ BORDER=1 ID=”results”>
<THEAD>
<TR BGCOLOR=”#A0CFEC”>
<TH ALIGN=left WIDTH=”25%”>
<ul>
<li><a href=”#”>Home<div></div><div id=”0_arrow”>▼</div></a>
<ul>
<li><a   href=”javascript:;”>Page home</a></li>
<li><a   href=”javascript:;”>Section home</a></li>
<li><a href=”javascript:void(0);” >Area home</a>
</li>
</ul>
</li>
</ul>
</TH>
<TH ALIGN=left WIDTH=”25%”>
<ul>
<li><a href=”#”>Products<div></div><div  id=”1_arrow”>▼</div></a>
<ul>
<li><a   href=”javascript:;”>Scissor</a></li>
<li><a   href=”javascript:;”>Knife</a></li>
<li><a      href=”javascript:;”>Cutlery</a>
<li><a   href=”javascript:;”>Chain</a></li>
<li><a   href=”javascript:;”>Prounes</a></li>
<li><a      href=”javascript:;”>Iron sticks</a>
</li>
</ul>
</li>
</ul>
</TH>
<TH ALIGN=left WIDTH=”25%”>
<ul>
<li><a href=”#”>Services<div></div><div id=”2_arrow”>▼</div></a>
<ul>
<li><a   href=”javascript:;”>Home Repairs</a></li>
<li><a   href=”javascript:;”>Auto Repairs</a></li>
<li><a   href=”javascript:;”>Computer Repairs</a></li>
<li><a   href=”javascript:;”>Phone Repairs</a></li>
<li><a   href=”javascript:;”>Mobile Repairs</a></li>
</ul>
</li>
</ul>
</TH>
<TH ALIGN=left WIDTH=”25%”>
<ul>
<li><a href=”#”>Contact US<div></div><div id=”3_arrow”>▼</div></a>
<ul>
<li><a   href=”javascript:;”>Email</a></li>
<li><a   href=”javascript:;”>Phone</a></li>
<li><a      href=”javascript:;”>Mail</a></li>
<li><a      href=”javascript:;”>In-person</a></li>
</ul>
</li>
</ul>
</TH>
</TR>
</THEAD>

</TABLE>
</FORM>
</BODY>
</HTML>

HTML Table Column Sorting

Here I’m showing a simplest way of how to make each column do sorting in ascending and descending orders using simple javascript (you may definitely get an easy way of doing this from jQuery). But this one surely useful for some people at least.

to see the results, copy/download the code and open this in your browser, and click on any header’s uparrow and downarrow icons, then you see the results.
javascript code:-
<code>
function Compare(a, b) {
var aVal = a.value;
var bVal = b.value;
return (aVal == bVal ? 0 : (aVal &gt; bVal ? 1 : -1));
}
function CompareNumbers(a, b) {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
return (aVal – bVal);
}
function sortTable(sortOn,sortOrder) {
var table = document.getElementById(‘results’);
var tbody = table.getElementsByTagName(‘TBODY’)[0];
var thead = table.getElementsByTagName(‘THEAD’)[0];
var rows = tbody.getElementsByTagName(‘TR’);
var rowArray = new Array();
for (var i=0, length=rows.length; i&lt;length; i++) {
rowArray[i] = new Object;
rowArray[i].oldIndex = i;
rowArray[i].value = rows[i].getElementsByTagName(‘td’)[sortOn].firstChild.nodeValue;
if (rowArray[i].value == null){
var firstChild=rows[i].getElementsByTagName(‘td’)[sortOn].firstChild;
rowArray[i].value=firstChild.toString().substring(26, 35);
}
}
if(sortOrder==’dsc’){
if (sortOn==0){
rowArray.sort(CompareNumbers);
}else{
rowArray.sort(Compare);
}
rowArray.reverse();
}else{
if (sortOn==0){
rowArray.sort(CompareNumbers);
}else{
rowArray.sort(Compare);
}
}
var newTbody = document.createElement(‘tbody’);
for (var i=0, length=rowArray.length ; i&lt;length; i++) {
var row=rows[rowArray[i].oldIndex].cloneNode(true)
row.bgColor=”white”
if(i%2==1)
row.bgColor=”#FAF8CC”;
newTbody.appendChild(row);
}
var L=tbody.length;
while(L)table.removeChild(tbody[–L]);
var root=document.getElementById(‘ITable’);
var tab=document.createElement(‘table’);
tab.className=”mytable”;
tab.setAttribute(“WIDTH”,”600″);
tab.setAttribute(“border”,”1″);
tab.setAttribute(“ID”,”results”);
tab.appendChild(thead);
tab.appendChild(newTbody);
root.removeChild(table);
root.appendChild(tab);
}
</code>
CSS code:-
a{
font-weight: bold;
font-family: Times New Roman;
font-style: normal;
font-size: 15px;
color: black;
text-decoration: none;
display: inline;
margin: 1;
}

Here is complete HTML code to:
<HTML>
<HEAD>
<TITLE>
HTML table sorting
</TITLE>
<SCRIPT TYPE=”text/javascript”>
function Compare(a, b) {
var aVal = a.value;
var bVal = b.value;
return (aVal == bVal ? 0 : (aVal > bVal ? 1 : -1));
}
function CompareNumbers(a, b) {
var aVal = parseInt(a.value);
var bVal = parseInt(b.value);
return (aVal – bVal);
}
function sortTable(sortOn,sortOrder) {
var table = document.getElementById(‘results’);
var tbody = table.getElementsByTagName(‘TBODY’)[0];
var thead = table.getElementsByTagName(‘THEAD’)[0];
var rows = tbody.getElementsByTagName(‘TR’);
var rowArray = new Array();
for (var i=0, length=rows.length; i<length; i++) {
rowArray[i] = new Object;
rowArray[i].oldIndex = i;
rowArray[i].value = rows[i].getElementsByTagName(‘td’)[sortOn].firstChild.nodeValue;
if (rowArray[i].value  == null){
var firstChild=rows[i].getElementsByTagName(‘td’)[sortOn].firstChild;
rowArray[i].value=firstChild.toString().substring(26, 35);
}
}
if(sortOrder==’dsc’){
if (sortOn==0){
rowArray.sort(CompareNumbers);
}else{
rowArray.sort(Compare);
}
rowArray.reverse();
}else{
if (sortOn==0){
rowArray.sort(CompareNumbers);
}else{
rowArray.sort(Compare);
}
}
var newTbody = document.createElement(‘tbody’);
for (var i=0, length=rowArray.length ; i<length; i++) {
var row=rows[rowArray[i].oldIndex].cloneNode(true)
row.bgColor=”white”
if(i%2==1)
row.bgColor=”#FAF8CC”;
newTbody.appendChild(row);
}
var L=tbody.length;
while(L)table.removeChild(tbody[–L]);
var root=document.getElementById(‘IDMTable’);
var tab=document.createElement(‘table’);
tab.className=”mytable”;
tab.setAttribute(“WIDTH”,”600″);
tab.setAttribute(“border”,”1″);
tab.setAttribute(“ID”,”results”);
tab.appendChild(thead);
tab.appendChild(newTbody);
root.removeChild(table);
root.appendChild(tab);
}

</SCRIPT>

<STYLE TYPE=”text/css”>

a{
font-weight: bold;
font-family: Times New Roman;
font-style: normal;
font-size: 15px;
color: black;
text-decoration: none;
display: inline;
margin: 1;
}

</STYLE>

</HEAD>
<BODY>
</BR />
<H3>HTML Table with column Sorting</H3>
<FORM ACTION=”” METHOD=”POST” NAME=”searchForm”>

<BR />
<div id=”IDMTable” >
<TABLE WIDTH=”600″ BORDER=1 ID=”results”>
<THEAD>
<TR BGCOLOR=”#A0CFEC”>
<TH ALIGN=left WIDTH=”25%”>UID &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href=”#” onclick=”sortTable(0,’asc’);” href=”javascript:;”>&uArr;</a>
<a href=”#” onclick=”sortTable(0,’dsc’);”>&dArr;</a>
</TH>
<TH ALIGN=left WIDTH=”25%”>NID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href=”#” onclick=”sortTable(1,’asc’);” href=”javascript:;”>&uArr;</a>
<a href=”#” onclick=”sortTable(1,’dsc’);”>&dArr;</a>
</TH>
<TH ALIGN=left WIDTH=”25%”>First Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href=”#” onclick=”sortTable(2,’asc’);” href=”javascript:;”>&uArr;</a>
<a href=”#” onclick=”sortTable(2,’dsc’);”>&dArr;</a>
</TH>
<TH ALIGN=left WIDTH=”25%”>Last Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a href=”#” onclick=”sortTable(3,’asc’);” href=”javascript:;”>&uArr;</a>
<a href=”#” onclick=”sortTable(3,’dsc’);”>&dArr;</a>
</TH>
</TR>
</THEAD>
<TBODY>

<TR BGCOLOR=>
<TD>45103</TD>
<TD>skaba</TD>
<TD>Sandra</TD>
<TD>Kabagambe</TD>
</TR>

<TR BGCOLOR=#FAF8CC>
<TD>27057</TD>
<TD>skale</TD>
<TD>Su</TD>
<TD>Kalender</TD>
</TR>

<TR BGCOLOR=>
<TD>64559</TD>
<TD>skalle</TD>
<TD>Sylvia</TD>
<TD>Kalley</TD>
</TR>

<TR BGCOLOR=#FAF8CC>
<TD>13235</TD>
<TD>rimsa</TD>
<TD>Rhin</TD>
<TD>Imsail</TD>
</TR>

<TR BGCOLOR=>
<TD>46662</TD>
<TD>namile</TD>
<TD>Nancy</TD>
<TD>Amile</TD>
</TR>

<TR BGCOLOR=#FAF8CC>
<TD>31805</TD>
<TD>mmclure</TD>
<TD>Maria</TD>
<TD>Mclure</TD>
</TR>

</TBODY>
</TABLE>
</div>
</FORM>
</BODY>
</HTML>

My Review of Roku HD Player

Originally submitted at Roku

The new HD, with built-in wireless, delivers top value in high-definition streaming.

I love this as I use these kind newly

By Sumi from Herndon, VA on 1/12/2011

 

4out of 5

Pros: Video selection, Built in Wi-Fi, Saving money to watch TV, Easy to use, Easy to set up

Cons: No subtitles in movies, Want more video choices, No switch off

Best Uses: Primary TV, Living room

Describe Yourself: Netflix fan, Movie buff, Home entertainment enthusiast

I can’t say anything about this since I’m new user to use newly digital video player, but I love being used this but some of the channels are not coming properly I don’t know whether they are wrong with ROKU or their service.
2) If you provide subtitiles or closed captioning for Netflix kind videos, then I like it very much more.

(legalese)

->File Open/Accessing from Java Web Application(JSP/Servlet)

Basically this thread shows you how to open a file from your physical storage (outside of web container), in general, a client can access files from within your container but can not access from within your physical storage by a relative URI.

This post is is to create a Servlet  which loads a file from a path outside of the web container and then streams the file to the HttpServletResponse. The client should get a ‘Save as’ popup dialogue. You can pass the file name or the file ID as a part of the request URI. You can also consider to pass it as a request parameter, but that would cause problems with getting the filename right during saving in certain web browsers (Internet Explorer and so on).

In this post, this.filePath=”/pics/lates”, given a path to get images from the “/pics/lates” path.

In Windows environment if the application server is running in C: drive, the above path is exactly same as “C:\pics\lates”.

In UNIX/LINUX, it is just straight forward “/pics/lates”.

If you stored files in WebContent of a war, for ex: “/WEB-INF/pics/latest” folder, then you can retrieve the absolute path with

this.filePath=getServletContext().getRealPath(“/WEB-INF/pics/latest”);

Here is a basic example of a FileOpenServlet which serves a file from a path outside of the web container.

package ex;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The File servlet for serving from absolute path.
 * @author Sumant
 *
 */
public class FileOpenServlet extends HttpServlet {

    private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
    private String filePath;
    public void init() throws ServletException {
        this.filePath = "/pics/latest";
    }

   /**
     * Do your thing if the file is not supplied to the request URI.
     * Throw an exception, or send 404, or show default/warning page, or just ignore it.
     */

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String requestedFile = request.getPathInfo();
if (requestedFile == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Decode the file name (might contain spaces and on) and prepare file object.
File file = new File(filePath, URLDecoder.decode(requestedFile, "UTF-8"));

// Check if file actually exists in filesystem.
if (!file.exists()) {
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}

// Get content type by filename.
String contentType = getServletContext().getMimeType(file.getName());
// If content type is unknown, then set the default value.
if (contentType == null) {
contentType = "application/octet-stream";
}

// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;

try {
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

// Write file contents to response.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} finally {
close(output);
close(input);
}
}

private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

In order to get the FileOpenServlet to work, add the following entries to the Web Deployment Descriptor web.xml:
<servlet>
     <servlet-name>fileOpenServlet</servlet-name>
     <servlet-class>ex.FileOpenServlet</servlet-class>
</servlet>
<servlet-mapping>
     <servlet-name>fileOpenServlet</servlet-name>
     <url-pattern>/pics/latest/*</url-pattern>
</servlet-mapping>

Note:- You can change the <url-patter> as you like.
A simple JSP body to get files:
<a href="pics/latest/DSC0001.jpg">download DSC0001.jpg</a>
<a href="pics/latest/bar.jpg">download bar.jpg</a>

« Older entries