background preloader

Network

Facebook Twitter

TCP/IP Client and Server. Sockets can be configured to act as a server and listen for incoming messages, or connect to other applications as a client. After both ends of a TCP/IP socket are connected, communication is bi-directional. Echo Server This sample program, based on the one in the standard library documentation, receives incoming messages and echos them back to the sender. It starts by creating a TCP/IP socket. import socketimport sys # Create a TCP/IP socketsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) Then bind() is used to associate the socket with the server address. . # Bind the socket to the portserver_address = ('localhost', 10000)print >>sys.stderr, 'starting up on %s port %s' % server_addresssock.bind(server_address) Calling listen() puts the socket into server mode, and accept() waits for an incoming connection. # Listen for incoming connectionssock.listen(1) while True: # Wait for a connection print >>sys.stderr, 'waiting for a connection' connection, client_address = sock.accept()

Sockets in Python: Into the World of Python Network Programming. “Code less, achieve more” is the prime philosophy behind the development of all the Very High Level Languages (or VHLL in short). But a fewer number of lines should not mean reduced flexibility in terms of choosing an approach for solving a problem. Though many of the VHLL, or scripting languages as they are popularly known, do not keep flexibility in mind the flexibility, there are a few that have the logic of flexibility and choice as their core. Python is one of them. This fact is evident if one tries to do network programming in Python.

There are plenty of choices for the programmer. These range from low-level sockets or raw-sockets to a completely extensible and functional web server. In this tutorial I will be discussing how to use raw sockets to create network oriented applications in Python. Sockets and Ports -- Doing it the Python way Sockets and ports form the core of any network oriented application. Unix Domain/ File-system Domain Internet Domain 1. Basic TCP/IP Socket Programming in Python. To follow me in this article, you will have to have python installed. I've covered this in a previous article here on AC. I'm sure you can find it :) The code written in the article is cross platform at least between Windows XP and Linux.

I recommend you write the code in a text editor which supports proper indentation and syntax highlight, for example IDLE (which was covered in a previous article) or vim. First thing's first. Import socket and should be around the top of your code, as we will want to use the library soon. Now we can use s for all our connections, to send data through s we would use s.send() to receive we would use s.recv(). Since HTTP most commonly uses port 80, that's the port I use when connecting to Google. S.send("GET /\n") This commands will send a HTTP command ("GET") followed by a parameter (the slash) to the server, we also append a new line (\n) at the end, to make sure the server interprets this as a single command. The full code should now look like this: Utilisation des sockets avec python. Mai 24 Python n’est pas seulement un langage pour faire des traitements sur les chaînes de caractères, mais aussi pour construire des applications beaucoup plus intéressantes.

Dans cet article je vais essayer d’ullistrer l’utilisation des socket avec python à travers quelques lignes de code. Avec un minimum de bagage en TCP/IP, tout le monde sais que pour établir une connexion entre deux machines ils nous faut d’abord un protocole de transfert de donnée, puis une trame qui va envoyer notre information et bien sur l’adresse des deux machines, chacune de ces deux machines doit impérativement avoir un port ouvert pour établir la communication.

Dans cet exemple on va utiliser python pour faire un script simple avec deux fonctions la première va scanner les ports ouverts d’une ip donnée et les afficher, la deuxième va essayer d’établir une connexion avec un serveur Web et de nous renvoyer son ‘Header’. le script est en GPL et il est là pour des raisons purement educatives. Voila ! A Guide to Network Connectivity in Linux. HTTP Made Really Easy. A Practical Guide to Writing Clients and Servers Home > Web Technology Made Really Easy > HTTP Made Really Easy Donate Table of Contents|Footnotes December 10, 2012-- Updated the links about robots. HTTP is the network protocol of the Web. This tutorial explains the simple, English-based structure of HTTP communication, and teaches you the practical details of writing HTTP clients and servers. Since you're reading this, you probably already use CGI. The whole tutorial is about 15 printed pages long, including examples.

Before getting started, understand the following two paragraphs: Writing HTTP or other network programs requires more care than programming for a single machine. OK, enough of that. Top of Page Using HTTP 1.0 Upgrading to HTTP 1.1 Appendix The HTTP Specification Several related topics are discussed on a "footnotes" page: What is HTTP? HTTP stands for Hypertext Transfer Protocol. What are "Resources"? HTTP is used to transmit resources, not just files. Return to Table of Contents Notes: Le réseau.