Python issue with using Windows Path to create hyperlink in email - python

I have a path in the format of:
\\server1\path1\path2\path3
I'm using the python email module to place this path in a hyperlink, the html portion of my code looks like
#import Email modules etc...
message=MIMEMultipart()
message['From']="me"
message['To']="someone"
message['Subject']="Stuff"
html = """\
<html>
<head></head>
<body>
<p>
Here is the link<br>
View Folder_"""+str(datetime.datetime.now())[:10]+"""
</p>
</body>
</html>
"""
part1= MIMEText(html,'html')
message.attach(part1)
mailer=smtplib.SMTP("something.server")
mailer.sendmail(message['From'],message['To'],message.as_string())
The email sends without any problems...but in the message body there is no link, the recipient just sees
Here is the link
View Folder_today's date (but no hyperlink pointing to the folder)
Any suggestions would be appreciated..I'm just suspecting this may be due to the Windows File path, but I'm not sure.

Related

Opening a file with Python in the browser [duplicate]

This question already has answers here:
How to copy / download file created in Pyodide in browser?
(2 answers)
Closed 9 months ago.
I am doing a project with an html, js and css front-end and a python back-end.
I linked back-end and front-end with the py-script library.
I have to open a json file with the python script and it works, the problem is that it doesn't open the file where i want... it stores the file in this directory: /home/pyodide/file.json
How can I select the location where the file opens/gets created?
by the way, the code I am working with is this:
python
open("my_file.json")
# and it doesn't open it in the same directory as the python file who created it
The file /home/pyodide/file.json is located within the browser virtual file system. That file system is not part of your local disk storage. The virtual file system is not persistent. When you refresh the page any files stored are lost.
There is a set of FS APIs that manage the virtual file system which includes the ability to mount an IDBFS virtual file system for persistent storage. However, that is not the feature you are looking for.
Python applications have the same restrictions that JavaScript applies have. Neither can directly access the local file system. Applications must request access from the web browser and read data from the web browser. This is performed using HTML INPUT elements. The actual file access is performed by the FileRead class.
[UPDATE] Tonight I published an article on Pyscript Files and File Systems that explains in more detail with several examples.
This example is available on my website to experiment with: File Example
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
<title>File Example</title>
</head>
<body>
<p>This example shows how to read a file from the local file system and display its contents</p>
<br />
<p>Warning: Not every file type will display. PyScript removes content with tags such as XML, HTML, PHP, etc. Normal text files will work.</p>
<br />
<p>No content type checking is performed to detect images, executables, etc.</p>
<br />
<label for="myfile">Select a file:</label>
<input type="file" id="myfile" name="myfile">
<br />
<br />
<div id="print_output"></div>
<br />
<p>File Content:</p>
<div style="border:2px inset #AAA;cursor:text;height:120px;overflow:auto;width:600px; resize:both">
<div id="content">
</div>
</div>
<py-script output="print_output">
import asyncio
from js import document, FileReader
from pyodide import create_proxy
async def process_file(event):
fileList = event.target.files.to_py()
for f in fileList:
data = await f.text()
document.getElementById("content").innerHTML = data
def main():
# Create a Python proxy for the callback function
# process_file() is your function to process events from FileReader
file_event = create_proxy(process_file)
# Set the listener to the callback
e = document.getElementById("myfile")
e.addEventListener("change", file_event, False)
main()
</py-script>
</body>
</html>

Open a xml from url (python)

I am trying to parse an xml from an url.
So originaly my code looked like this:
from xml.dom import minidom
xmldoc = minidom.parse('all.xml')
In this case the xml what I intend to parse was on my local harddrive and it worked out perfectly!
The next step was to parse a xml from an url.
A sensormeter from allnet constantly inserts xml data into the networkt which is over the following url with a browser accessible: 192.168.60.242/xml
this is the xml:
<html>
<head>
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
</head>
<body>
<form>
<textarea rows="50" cols="132"><xml><data> <devicename>ALL4000</devicename> <n0>0</n0><t0> 2.39</t0><min0> 0.00</min0><max0> 2.55</max0><l0>-55</l0><h0>150</h0><s0>102</s0> <n1>1</n1><t1> 2.53</t1><min1> 2.32</min1><max1> 10487.04</max1><l1>-55</l1><h1>150</h1><s1>102</s1> <n2>2</n2><t2> 2.55</t2><min2> 0.00</min2><max2> 2.55</max2><l2>-55</l2><h2>150</h2><s2>102</s2> <n3>3</n3><t3>-20480.00</t3><min3> 0.00</min3><max3> 5580.80</max3><l3>-55</l3><h3>150</h3><s3>0</s3> <n4>4</n4><t4>-20480.00</t4><min4> 40.96</min4><max4> 41943.04</max4><l4>-55</l4><h4>150</h4><s4>0</s4> <n5>5</n5><t5>-20480.00</t5><min5> 10.24</min5><max5> 0.08</max5><l5>-55</l5><h5>150</h5><s5>0</s5> <n6>6</n6><t6>-20480.00</t6><min6> 0.00</min6><max6>-20480.00</max6><l6>-55</l6><h6>150</h6><s6>0</s6> <n7>7</n7><t7>-20480.00</t7><min7> 0.00</min7><max7> 0.00</max7><l7>-55</l7><h7>150</h7><s7>0</s7> <n8>8</n8><t8>-20480.00</t8><min8> 336855.04</min8><max8> 1342177.28</max8><l8>-55</l8><h8>150</h8><s8>0</s8> <n9>9</n9><t9>-20480.00</t9><min9> 0.00</min9><max9> 0.00</max9><l9>-55</l9><h9>150</h9><s9>0</s9> <n10>10</n10><t10>-20480.00</t10><min10> 0.00</min10><max10> 0.00</max10><l10>-55</l10><h10>150</h10><s10>0</s10> <n11>11</n11><t11>-20480.00</t11><min11> 0.00</min11><max11> 0.00</max11><l11>-55</l11><h11>150</h11><s11>0</s11> <n12>12</n12><t12>-20480.00</t12><min12> 0.00</min12><max12> 0.00</max12><l12>-55</l12><h12>150</h12><s12>0</s12> <n13>13</n13><t13>-20480.00</t13><min13> 0.00</min13><max13> 0.00</max13><l13>-55</l13><h13>150</h13><s13>0</s13> <n14>14</n14><t14>-20480.00</t14><min14> 0.00</min14><max14> 0.00</max14><l14>-55</l14><h14>150</h14><s14>0</s14> <n15>15</n15><t15>-20480.00</t15><min15> 0.00</min15><max15> 0.00</max15><l15>-55</l15><h15>150</h15><s15>0</s15> <fn0>1</fn0><ft0>0</ft0><fs0>0</fs0> <fn1>2</fn1><ft1>0</ft1><fs1>0</fs1> <fn2>3</fn2><ft2>0</ft2><fs2>0</fs2> <fn3>4</fn3><ft3>0</ft3><fs3>0</fs3> <fn4>5</fn4><ft4>0</ft4><fs4>0</fs4> <fn5>6</fn5><ft5>0</ft5><fs5>0</fs5> <fn6>7</fn6><ft6>0</ft6><fs6>0</fs6> <fn7>8</fn7><ft7>0</ft7><fs7>0</fs7> <fn8>9</fn8><ft8>0</ft8><fs8>0</fs8> <fn9>10</fn9><ft9>0</ft9><fs9>0</fs9> <fn10>11</fn10><ft10>0</ft10><fs10>0</fs10> <fn11>12</fn11><ft11>0</ft11><fs11>0</fs11> <fn12>13</fn12><ft12>0</ft12><fs12>0</fs12> <fn13>14</fn13><ft13>0</ft13><fs13>0</fs13> <fn14>15</fn14><ft14>0</ft14><fs14>0</fs14> <fn15>16</fn15><ft15>0</ft15><fs15>0</fs15> <rn0>0</rn0><rt0>0</rt0> <rn1>1</rn1><rt1>0</rt1> <rn2>2</rn2><rt2>0</rt2> <rn3>3</rn3><rt3>0</rt3> <it0>248</it0><it1>255</it1><it2>255</it2><it3>255</it3><it4>128</it4><it5>1</it5><it6>255</it6><it7>255</it7> <date>05.08.2006</date><time>07:54:10</time><ad>1</ad><ntpsync>-1</ntpsync><i>10</i><f>0</f> <sys>18773719</sys><mem>24760</mem><fw>2.89</fw><dev>ALL4000</dev> <sensorx>5</sensorx><sensory>3</sensory> </data></xml> </textarea>
</form>
</body>
</html>
So I changed the code into this:
import urllib
from xml.dom import minidom
xmldoc = minidom.parse(urllib.urlopen('https//192.168.60.242 /xml')).read()
Unfortunatelly it does not work.
If executed, This is what gets returned:
File "/usr/lib/python2.7/urllib.py" line 86, in urlopen return
opener.open(url)
IOError: Errno socket error Errno 111 Connection refused
I hope someone can help me out on this.
thanks
Try pinging your website from your code and make sure you don't have login privileges, log in to your admin cp and make sure your xml page allows users to read or change its permissions.
Alternatively, try this-
xmlUrl= urllib2.urlopen("http://192.168.60.242/xml" )
xmlUrl_string= xmlUrl.read()
parser.parseString( xmlUrl_string)

CGI Module - Python

I found this CGI Module, its letting me use HTML tags inside a python script.
ive seen some topics in here that shows how to use it, but when im using it it doesnt works.
import cgi
print ("""
<html>
<body>
Hello
</body>
</html>
""")
and this is the output when im running the script:
<html>
<body>
Hello
</body>
</html>
how can i use this properly?
thanks.
If you have your CGI script already hooked up to a web server, you will need to emit the HTTP headers too, e.g.
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
print ("""
<html>
<body>
Hello
</body>
</html>
""")
Note that the cgi module is not being used in any way to achieve this; just simple calls to print(). The module is useful when you want to process form data submitted by a client through a HTML form.

Translating Python print statements to Django

I am in the process of creating a Django web application that reads a URL and outputs selected data from the page. I have written the code in Python that parses the web page and it currently returns the information that I need to display in the Django app as desired.
Before I dive in I just want to confirm what I have researched is correct as I only have a limited time to complete the project.
To summarise my python code, it is in the src folder in a class called "manage.py"
I have created print statements that print the information that I need to display (I did this to ensure it was returning the correct data)
print(variable1)
print("some text" + variable2)
Can I create the Django app code in the same file, "manage.py"? (The project has already been created as a Django app in Eclipse when I started building the project)
Would I build the Django code as I've estimated below if I'm using the variables defined from the Python code above?
<!DOCTYPE>
<html>
<head>
<title>{% block title %}Title of website{% endblock %}</title>
</head>
<body>
<h1>Web page report</h1>
<h2>Summary of web page</h2>
<h3>Title of document</h3>
<p>{{variable1}}</p>
<h3>The file size of the document</h3>
<p>{"Some text" + {variable2}}</p>
</body>
</html>
Django has strict rules about where to place which information. You can not write everything into manage.py. Answering requests from the browser is for example done using view functions.

How to loading/show a picture in a Python CGI page

I have written a little survey using Python and CGI. I am trying to show a picture using the normal <img> tag, But even-though the picture is in the same directory as my cgi script, my script cannot show it. I also changed the header to this:
print "Content-type: text/html; image/jpeg"
print
print """<html>
<head>
<title>You are going to be redirected</title>
</head>
<body bgcolor = #14b585>
<br>
<p align=center><font>Helloooo</font></p>
<img src="cat.jpeg" alt="cat" width="304" height="228"/>
<form action="./sample.py" method="post">
<p align=center><input type="submit" value="YES!" /></p>
</form>
</body>
</html>
"""
Why?(it is a very small jpg file)
print "Content-type: text/html; image/jpeg"
Don't change the header to that. You can't have multiple content types for a single document (multipart documents excluded, but browsers don't support them and that isn't the right format).
You are delivering an HTML document with an reference to an image in it. The image will be a separate request and response.
print "Content-type: text/html"
Or, better:
print "Content-type: text/html; charset=utf-8"
(Assuming you are using utf-8, which you should be).
print """<html>
Your Doctype is missing. This will trigger quirks mode, which is undesirable. You also have a great deal of legacy presentational markup that should be replaced by CSS.
<img src="cat.jpeg" alt="cat" width="304" height="228"/>
The context suggests that the image is decorative, so the alternative text should probably be "" (see Alt texts in IMGS), but there is nothing wrong with the actual reference to the image.
But even-though the picture is in the same directory as my cgi script
Since the HTML seems to be OK. You need to check that the image is.
Can you reach the image by typing the URL in directly?
What do the web server logs say when you try?
It is possible that your server is configured to try to treat everything in the directory as an executable, no matter what the file extension, so it might be trying to run the image as if it were a CGI program (which will obviously fail). If so, then you could either move the image or change the configuration of the server.
And I've just noticed this comment:
I did this in my browser: localhost/cgi-bin/cat.jpg and it got an error, I checked the logs, it Exec format error: exec of '/home/hossein/public_html/cgi-bin/cat.jpg' failed
That is what is happening. Moving the image is the simplest solution.
your apache was configured to use the cgi-bin directory as an CGI scripts folder, so any request that trying to get a file from this folder apache try to execute it as an CGI script. to make your image visible move it to the www/html folder.

Categories