Recently, I have been working on some python (2) programs that require me to connect to a 'https' site. To do this I have been using the ssl module to wrap sockets etc.
import ssl
ssl_socket = ssl.wrap_socket(...)
I have also been using the urllib2 module to do this automatically, which to my knowledge, requires httplib and ssl etc. However, just today, I have been using it again exactly in the same way but now a lot of the functions in the ssl module have just... well, disappeared? On my other Linux machine when I execute:
import ssl
dir(ssl)
It brings back at least 50 odd classes and sub functions for it such as "wrap_socket" and "_create_default_https_context"; functions that are required for me at the moment. Now however, since today, I have been using it in exactly the same way, which now for some reason does not have the functions "wrap_socket" or "_create_default_https_context". And also now when I execute:
import ssl
dir(ssl)
It only brings back:
['__builtins__', '__doc__', '__file__','__name__','__package__','extractor', 'httplib', 're', 'request', 'socket', 'ssl']
Instead of about 10X more functions than when it was working? I have tried reinstalling python, updating it, reinstalling the whole machine (Linux) and the same thing happens. I am very lost as to why this is happening and it is a pain because the urllib2 and httplib modules require the ssl module to work on "https" sites. Any help would be really appreciated. Thanks :) Pete.
Related
I have recently installed the pexpect 4.0 module, as it will be quite useful for the program I am creating. I do have windows, so I looked specifically at the exceptions for pexpect, knowing that the normal spawn and run methods aren't available to me. But, I cant get the "windows methods" that the module is supposed to show windows users, which are:
pexpect.popen_spawn.PopenSpawn, and pexpect.fdpexpect.fdspawn.
Does anyone have any idea how I can get these methods? I am running on windows 10, python 3.4.
Side note: I am currently working on trying to get winpexpect to import the spawn module from pexpect, but I am also failing at that as well.
This will do it for you:
from pexpect import popen_spawn, fdpexpect
Then you can do the rest of what you needed.
edit: this is the reason you did not see it, in the __init__.py notice this:
__all__ = ['ExceptionPexpect', 'EOF', 'TIMEOUT', 'spawn', 'spawnu', 'run','runu', 'which', 'split_command_line', '__version__', '__revision__']
import sys
import os
import logging
# need to add environment to apache's path for includes
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "../")))
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".")))
# likewise add cherrypy/other modules
sys.path.append(os.path.abspath("/Library/Python/2.7/site-packages/"))
import requests
response = requests.get('http://www.google.com').text
Using Python 2.7.6, requests 2.7.0, and Apache under MAMP 3.0, the above code crashes. A quick look through the code using winpdb seems to suggest that actually trying to open an internet connection is what is crashing the Python process. The Apache log is not very helpful, only saying
[notice] child pid 18879 exit signal Segmentation fault (11)
While my full code uses Cherrypy 3.8 to provide the WSGI portion of the framework, I feel that it is irrelevant to the problem at hand.
Is this some known problem with requests+apache, or is it some other problem? Python crashing without any comments makes it hard for me to even think of a way to start solving this issue.
EDIT: Using pdb, I found that the program segfaults on line 1421 of urllib.py in the python standard library.
proxy_settings = _get_proxy_settings()
where _get_proxy_settings comes from _scproxy.
I still have no idea how to fix this.
It seems that this is a platform-specific bug for my version of MacOS.
I'm trying to write a program that uses the scapy modules. I'm using PyDev for my development but it keeps giving me errors when I import certain parts of the Scapy module. I'm pretty sure I have my import paths in PyDev set up correctly. I've looked at some of the other questions involving "Unresolved Import" errors on here. However, none of the suggestions I saw seemed to help.
The weird thing is that it is only part of the scapy modules that don't work. So for instance PyDev doesn't complain when I do
from scapy.all import Ether, sendp
However, when I do
from scapy.all import IP, UDP
I get errors.
I thought maybe I was importing the wrong modules but when I go to the interpreter and type in the second example it gives no errors and then I can create IP packets using IP(params), which is what I'm trying to do in my program.
I installed scapy using the ubuntu repositories, but when I started having import problems I downloaded the latest version from scapy.net and used the setup script. I even copied the zip and put it in my /usr/local/lib/python2.7/site-packages folder and added it to my python path in PyDev. But nothing seems to get rid of the error.
Any suggestions on what could be causing this and how to fix it?
Have you tried adding 'scapy' to the forced builtins? See: http://pydev.org/manual_101_interpreter.html for details.
I got a chance to play some more with this. I still don't know why PyDev gives me an import error when it works fine in the interpreter, however, I did find a way around it. To import things like IP, UDP, and TCP I'm now using the following
from scapy.layers.inet import IP, TCP, UDP
For non IPv4 stuff
from scapy.all import <Module Name>
seems to work just fine.
I have a Django instance that's mysteriously incapable of import memcache ... after (some unknown event that happens after a period of running just fine.)
LAMP configuration:
RHEL 5.7
Apache 2.2.3
mod_wsgi 2.3 (dynamically linked to Python2.5.4 .so)
Python 2.5.4
Django 1.2
memcache sits in: /usr/local/lib/python2.5/site-packages/python_memcached-1.44-py2.5.ee/memcache.pyc
If I open up a command shell, and import memcache, it imports just fine.
And for a time, in Django, import memcache works just fine.
But after some unknown event, the import fails: ImportError: No module named memcache
Just prior point of failure, I logged the system path, and the path explicitly includes /usr/local/lib/python2.5 and /usr/local/lib/python2.5/site-packages.
I also logged the response to pkgutil.iter_importers(), and found something interesting: At the point of failure, iter_importers features NONE of the zipimporters -- and it is a zipimporter that is required to look inside the egg and find memcache.
If I manually import memcache, it functions:
try:
import memcache
except ImportError:
import zipimport
zi = zipimport.zipimporter('/usr/local/lib/python2.5/site-packages/python_memcached-1.44-py2.5.egg')
memcache = zi.load_module('memcache')
What is going on? What can I do to make it work without the workaround?
Okay. I found the answer. There is a bug in Python 2.5's os.listdir C implementation on 64-bit computers. When I applied the patch, everything works permanently.
I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host.
I found bpgsql really good because it does not require an install, it's a single file that I can look at, read and then call the functions of. Does anybody know of something like this for MySQL?
MySQLdb is what I have used before.
If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are trying to do with it.
Another option may be to call your host and complain, or change hosts. Honestly these days, any self respecting web host that supports python and mysql ought to have MySQLdb pre installed.
I don't have any experience with http://www.SiteGround.com as a web host personally.
This is just a guess, but it's common for a shared host to support Python and MySQL with the MySQLdb module (e.g., GoDaddy does this). Try the following CGI script to see if MySQLdb is installed.
#!/usr/bin/python
module_name = 'MySQLdb'
head = '''Content-Type: text/html
%s is ''' % module_name
try:
__import__(module_name)
print head + 'installed'
except ImportError:
print head + 'not installed'
I uploaded it and got an internal error
Premature end of script headers
After much playing around, I found that if I had
import cgi
import cgitb; cgitb.enable()
import MySQLdb
It would give me a much more useful answer and say that it was not installed, you can see it yourself -> http://woarl.com/db.py
Oddly enough, this would produce an error
import MySQLdb
import cgi
import cgitb; cgitb.enable()
I looked at some of the other files I had up there and it seems that library was one of the ones I had already tried.
You could try setting up your own python installation using Virtual Python. Check out how to setup Django using it here. That was written a long time ago, but it shows how I got MySQLdb setup without having root access or anything like it. Once you've got the basics going, you can install any python library you want.
You really want MySQLdb for any MySQL + Python code. However, you shouldn't need root access or anything to use it. You can build/install it in a user directory (~/lib/python2.x/site-packages), and just add that to your PYTHON_PATH env variable. This should work for just about any python library.
Give it a shot, there really isn't a good alternative.
Take a pick at
https://docs.djangoproject.com/en/1.8/ref/databases/
MySQLdb is mostly used driver, but if you are using python3 and django 1.8.x that will not work, then you should use mysqlclient that is a folk of MySQLdb on the following link
https://pypi.python.org/pypi/mysqlclient