Send packet to webserver? - python

I'm just getting into python, and I dont now how to send packets.
So can anyone tell me how can I send packets to a webserver? I would also like to choose the size of the packet myself?
I'm using python 2.7.

You can use the httplib module, to easily create http requests. Or, if you want to take a bare approach, you can use the socket module to manually create a TCP connection and send whatever packets you would like.

from httplib import HTTPConnection
...
HTTPConnection.putheader("Content-Length","512")
HTTPConnection.endheaders()
HTTPConnection.send(data)
multipart/form-data example:
http://www.voidspace.org.uk/python/cgi.shtml#upload upload_test.py

Related

When do we need socket and when do we need request?

Why do we need socket despite of request library in python?
If we wanna socket to connect to other server so what is request library for?
Request is a higher level API for handling HTTP requests (which uses socket internally). There are dozens of other network protocols not covered by it. Of course, you could handle HTTP by using socket directly, but unless you have an extremely good reason to do so, you'd just be reinventing the wheel.
Requests is a Python HTTP library, whereas sockets are used for sending or receiving data on a computer network. HTTP is an application layer protocol that specifies how request and replies from client and server should be made. In socket programming, you make connection by specifying destination IP/Port and send your data to remote host.

sniffing http packets using scapy

Im using python 2.7.15, scapy and scapy-http on windows.
I want to sniff all the http packets and extract the html pages that were sent.
This is the code Im using:
from scapy.all import *
import scapy_http.http
def printPacket(packet):
if packet.haslayer('HTTP'):
print '='*50
print packet.show()
sniff(prn=printPacket)
but from some reason it only captures some of the http packets(when I use the browser I dont see any packets) and I dont see any html code in the ones that it does print.
I think that's because some of the traffic sent is HTTPS (= HTTP + TLS). In your function you expect to HTTP application layer, which is encapsulated and encrypted in a TLS layer, and therefore it is not matched.
To sniff HTTPS, you can use this: https://github.com/tintinweb/scapy-ssl_tls (I haven't tried it yet).

How to send a request by a private protocol with Python

I want to send a request to a server which is a private protocol based on TCP (Not HTTP), How Can I send a request using Python?
Python sockets are what you are looking for. Take a look at the Python Socket class at https://docs.python.org/2/library/socket.html
This site includes examples of how to set up a server and client and provides a basic example of the fundamental TCP Communication using Python. If you need something with more control you may want to look at Scapy: http://www.secdev.org/projects/scapy/
Scapy is a python implementation that provides a framework to control almost all aspects to Network Communication all the way down to Layer 2 (Ethernet Frame).

Impacket & dpkt sending features?

Is there any support for sending packets in impacket or dpkt libraries?
I was able to find examples of sniffing, interpreting and constructing packets using these libraries, but they don't seem to support sending over network interfaces.
Ping example of impacket library uses standard socket library in python to send the packet.
Any help would be great. Thanks
dpkt does not have any built-in way to send packets. Once you construct the packet, you will need to use a RAW socket to send out packets. Here is a good example, which shows you how to send a raw ICMP packet constructed using dpkt.

How would I handle multiple sockets and send data between them in Python 2.7.3?

I am trying to create a server in Python 2.7.3 which sends data to all client connections whenever one client connection sends data to the server. For instance, if client c3 sent "Hello, world!" to my server, I would like to then have my server send "Hello, world!" to client connections c1 and c2. By client connections, I mean the communications sockets returned by socket.accept(). Note that I have tried using the asyncore and twisted modules, but AFAIK they do not support this. Does anybody know any way to accomplish this?
EDIT: I have seen Twisted, but I would much rather use the socket module. Is there a way (possibly multithreading, possibly using select) that I can do this using the socket module?
You can absolutely do this using Twisted Python. You just accept the connections and set up your own handling logic (of course the library does not including built-in support for your particular communication pattern exactly, but you can't expect that).

Categories