how to install pcap in ubuntu using python - python

this is my code :
import pcap ,struct
pack=pcap.pcap()
pack.setfilter('udp')
key=''
for recv_time,recv_data in pack:
recv_len=len(recv_data)
if recv_len == 102 and recv_data[42]== chr(02) and recv_data[101] == chr(03):
print struct.unpack('>I',recv_data[49:53])[0]
print '登陆了'
elif recv_len == 55:
print struct.unpack('>I',recv_data[49:53])[0]
print '登陆了'
and i use this to install pcap :sudo apt-get install python-libpcap , it installed ,
but when i run the code , it show error :
Traceback (most recent call last):
File "weapon.py", line 2, in <module>
import pcap ,struct
ImportError: No module named pcap
what can i do ,
thanks

For me on Ubuntu 10.04 the package is called python-libpcap:
sudo apt-get install python-libpcap
Then when I do:
import pcap
It works great!

I'm guessing the installed module doesn't match the version of python you're running.
Look in /usr/lib/python2.7/dist-packages/ and see if pcap is there (substituting the version you're using for 2.7).

You need to make sure that you have pip installed.
sudo apt-get install python-pip
Then you can install pypcap:
sudo easy_install pypcap
sudo pip install pypcap
I verified it for Ubuntu 14.10.

Related

How to import the library function "import can" in Python

I am using Ubuntu. I want to spam the bus and view the packets using Wireshark. I tried to run the Python code shown below, but it threw the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'can'
My code:
import time, can
bustype = 'socketcan'
channel = 'vcan0'
def producer(id):
# :param id: Spam the bus with messages including the data id.
bus = can.interface.Bus(channel=channel, bustype=bustype)
for i in range(10):
msg = can.Message(arbitration_id=0xc0ffee, data=[id, i, 0, 1, 3, 1, 4, 1], extended_id=False)
bus.send(msg)
# Issue #3: Need to keep running to ensure the writing threads stay alive.
time.sleep(1)
producer(10)
Ubuntu 18.04 and later
In Ubuntu 18.04 and later python-can and python3-can are provided by the default Ubuntu repositories. Open the terminal and type:
sudo apt update
sudo apt install python3-can # for Python 3.x
or
sudo apt update
sudo apt install python python-can # for Python 2.x
To install CANard (Library for interacting with Controller Area Network (CAN)) you must use pip.
python3 -m pip install --user CANard # for Python 3.x
or
python -m pip install --user CANard # for Python 2.x
Whenever you get the no module named '<module_name>' error in Python, it means that python can not find the module. Likely because module is missing.
You can install python modules using pip.
If you don't have the pip tool then you can install it using sudo apt install python-pip on Debian based operating systems.
In your case you need python-can module which can be install by using
pip install python-can
You need to install external third-party package python-can
You can do so using pip. Follow the instructions in the provided link

Installing Scapy on a Mac: "ImportError: No module named pcapy"

I'm trying to run a python script that involves scapy but I can't seem to get it to run. I keep getting this error
ImportError: No module named pcapy
The script I'm trying to run is:
from scapy.all import *
def arp_display(pkt):
if pkt[ARP].op == 1: #who-has (request)
if pkt[ARP].psrc == '0.0.0.0': # ARP Probe
print "ARP Probe from: " + pkt[ARP].hwsrc
print sniff(prn=arp_display, filter="arp", store=0, count=10)
I've installed XCode, XQuartz, Python, and Scapy using macports
Please let me know what I'm missing! #noob
I had the same problem. I solved this using following steps:
1.) Open terminal and enter the command
sudo pip install --user pcapy
2.) Enter
python
in your terminal
3.) Enter the command
import pcapy
This should fix your problem.
Best regards,
Nazar Medeiros
Try installing libpcap and its Python wrapper from source, as listed here, though the latest version is 0.6.4 and not 0.6.2:
$ wget http://dfn.dl.sourceforge.net/sourceforge/pylibpcap/pylibpcap-0.6.4.tar.gz
$ tar xfz pylibpcap-0.6.4.tar.gz
$ cd pylibpcap-0.6.4
$ sudo python setup.py install
Download the latest version of pcapy from this link
https://www.coresecurity.com/corelabs-research/open-source-tools/pcapy
Unpack it and from the directory run the following command:
python setup.py install

Oursql insallation failing wtih "cython not found"

Trying to install oursql driver for python3x and sqlalchemy0.8 on ubuntu 12.10. It fails with the following error.
sudo pip-3.2 install oursql
Downloading/unpacking oursql
Running setup.py egg_info for package oursql
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build/oursql/setup.py", line 53
print "cython not found, using previously-cython'd .c file."
^
SyntaxError: invalid syntax
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 16, in <module>
File "/tmp/pip-build/oursql/setup.py", line 53
print "cython not found, using previously-cython'd .c file."
^
SyntaxError: invalid syntax
When I try to install cython I seem to already have it:
sudo pip-3.2 install cython
Requirement already satisfied (use --upgrade to upgrade): cython in /usr/local/lib/python3.2/dist-packages
Cleaning up.
What can I do to make it run?
Had the same error when running pip-3.2.
This is how I made it work:
Created my env using python-3.2:
virtualenv -p /usr/bin/python3.2
Installed the required packages:
sudo apt-get install python-dev
sudo apt-get install libmysqlclient-dev
Then installed:
sudo pip install cython
sudo pip install oursql
Edit1:
I was able to get pass you error with my above recomendations, but i was wrong (python3.2 was unable to read oursql). I tried the following and was able to make a connection:
First:
sudo apt-get install python3.2-dev
Then installed oursql for Python 3 from source (as suggested by it's maintainer here):
Get the Python 3 version of oursql from here and compile it from source (Don't have enough reputation to post the link, just go to oursql official site for installation instructions).
The maintainers have two packages, one for python 2.x and one for python 3.x, you should run:
pip install oursql3
There is nothing about cython.
You just encoutered a syntax error in print, because the print statement doesn't use brackets there. They were optional in Python 2's print statement, but are required in Python 3's print() function.
install it manually - reqs: python-dev, cython
then download oursql package (try 0.9.3.zip)
and
python setup.py install

hdf5 / h5py ImportError: libhdf5.so.7

I'm working on a project involving network messaging queues (msgpack, zmq, ...) on a RHEL 6.3 (x86_64) system. I was installing the most recent packages of glib, gevent, pygobject, pygtk, and such in order to get pylab / matplotlib to work (which hasn't been successful either).
After giving up I went back to my code and somehow I had managed to wreck my hdf5 / h5py installation - h5py can't find libhdf5.so.7 on import. I immediately reinstalled hdf5-1.8.9 in /usr/local/hdf5 on RHEL 6.3 (x86_64) as follows:
./configure --prefix=/usr/local/hdf5
make
make check
sudo make install
make check install
which seemed to work just fine. Then I went to reinstall h5py (in python 2.7.3):
python2.7 setup.py build --hdf5=/usr/local/hdf5/
python2.7 setup.py test # optional
# sudo python2.7 setup.py install
which fails to import the _errors file in the tests, like so:
======================================================================
ERROR: _hl.tests.test_attrs_data (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: _hl.tests.test_attrs_data
Traceback (most recent call last):
File "/usr/local/lib/python2.7/unittest/loader.py", line 252, in _find_tests
module = self._get_module_from_name(name)
File "/usr/local/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
__import__(name)
File "/home/cronburg/Downloads/h5py-2.0.1/build/lib.linux-x86_64-2.7/h5py/_hl/tests/test_attrs_data.py", line 5, in <module>
import h5py
File "/home/cronburg/Downloads/h5py-2.0.1/build/lib.linux-x86_64-2.7/h5py/__init__.py", line 1, in <module>
from h5py import _errors
ImportError: libhdf5.so.7: cannot open shared object file: No such file or directory
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (errors=12)
h5py was working fine before I went to install the aforementioned packages / tarballs, and I don't remember touching anything that even remotely looked like hdf5. Any ideas?
EDIT:
Trying to locate the file only shows it in the location i untarred it:
cronburg#rhel:~/Downloads/h5py-2.0.1$ locate libhdf5.so.7
/home/cronburg/tmp/hdf5-1.8.9/hdf5/lib/libhdf5.so.7
/home/cronburg/tmp/hdf5-1.8.9/hdf5/lib/libhdf5.so.7.0.3
/home/cronburg/tmp/hdf5-1.8.9/src/.libs/libhdf5.so.7
/home/cronburg/tmp/hdf5-1.8.9/src/.libs/libhdf5.so.7.0.3
take a look on:
http://rpm.pbone.net/index.php3/stat/3/srodzaj/1/search/libhdf5.so.7()(64bit)
Or should try this repo: https://ius.io/Packages/
I prefer always use the most updated python version, in a package format.
https://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/repoview/python27.html
sudo yum install -y https://centos6.iuscommunity.org/ius-release.rpm
sudo yum install -y python27
sudo yum install -y python27-devel
Do a pip install and be happy:
$ sudo pip install h5py
Installing collected packages: h5py
Successfully installed h5py-2.6.0
This also happened to me when using h5py on a clean raspbian. You need to install the system libraries first.
apt install libhdf5-dev
then
pip install h5py

I've installed xapian via macports, so why does this python app tell me that xapian needs to be installed?

I'm trying to run a python application on my system. When I try to execute it I get a traceback which ends with something saying I need to install Xapian. So I went ahead an installed xapian-core and xapian-bindings using macports. Then I tried to run the python application again, but got the same traceback. Any ideas about what I should do next? Is there some kind of additional xapian-python thingy I need to install? This kind of thing is all new to me.
I'm using Mac OS 10.6.3, Python 2.6.5 and Django 1.2.1
Here are the last few lines of the traceback I get:
File "/Users/nick/dev/ymtest/../ymtest/lib/haystack/__init__.py", line 46, in <module>
backend = load_backend(settings.HAYSTACK_SEARCH_ENGINE)
File "/Users/nick/dev/ymtest/../ymtest/lib/haystack/__init__.py", line 21, in load_backend
return __import__('haystack.backends.%s_backend' % settings.HAYSTACK_SEARCH_ENGINE, {}, {}, [''])
File "/Users/nick/dev/ymtest/../ymtest/lib/haystack/backends/xapian_backend.py", line 18, in <module>
raise MissingDependency("The 'xapian' backend requires the installation of 'xapian'. Please refer to the documentation.")
haystack.exceptions.MissingDependency: The 'xapian' backend requires the installation of 'xapian'. Please refer to the documentation.
The following worked for me after having the same issue on OS X 10.5:
make a macports selfupdate:
sudo port selfupdate
install python 2.6 over macports
sudo port install python26
install python_select
sudo port install python_select
select python 2.6 as system default
sudo python_select python26
install xapian-core
sudo port install xapian-core
install xapian-bindings WITH PYTHON VARIANT
sudo port install xapian-bindings +python26
copy all your python libs from "/Library/Python/2.5/site-packages" to
"/opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages"
wich is your new default site-packages folder!
restart Terminal
Done!

Categories