Traduction d'un programme C en python

Traduction d'un programme C en python - Python - Programmation

Marsh Posté le 23-03-2016 à 14:00:11    

Bonjour,
 
Voilà je suis un débutant en python et j'ai implémenté pour tester une connexion un client et un serveur en langage C. J'aimerais le traduire en python mais j'ai un peu de mal.
 
client.c

Code :
  1. //verifie qu'un appel systeme s'est bien passe  
  2. void try(int resultat, char * name){
  3.   if (resultat < 0){
  4.     perror(name);
  5.     exit(1);
  6.   }
  7. }
  8. int main(int ac, char * av[]){
  9.   struct addrinfo souhait, * info;
  10.   int sock, t;
  11.   char buffer[1024];
  12.   if (ac != 3){
  13.     fprintf(stderr, "usage: %s machine port\n", av[0]);
  14.     exit(1);
  15.   }
  16.   // formatter l'adresse
  17.   memset(&souhait, 0, sizeof souhait);
  18.   souhait.ai_family = AF_UNSPEC;
  19.   souhait.ai_socktype = SOCK_STREAM;
  20.   if ((t = getaddrinfo(av[1], av[2], &souhait, &info)) != 0){
  21.     fprintf(stderr, "erreur %s sur %s:%s\n", gai_strerror(t), av[1], av[2]);
  22.     exit(1);
  23.   }
  24.   try(sock = socket(info->ai_family, info->ai_socktype, info->ai_protocol), "socket" );
  25.   try(connect(sock, info->ai_addr, info->ai_addrlen), "connect" );
  26.   while((t = read(0, buffer, sizeof buffer)) > 0){
  27.     write(sock, buffer, t);
  28.     if ((t = read(sock, buffer, sizeof buffer)) <= 0)
  29.       break;
  30.     write(1, buffer, t);
  31.   }
  32.   try(t, "read" );
  33.   exit(t < 0);
  34. }


 
serveur.c
 

Code :
  1. int global_sock;
  2. char *traiter(char in[], int ln){
  3. in[ln] = 0;
  4. return in;
  5. }
  6. //verifie qu'un appel système s'est bien passé
  7. static void
  8. try(int resultat, char * name){
  9.   if (resultat < 0){
  10.     perror(name);
  11.     exit(1);
  12.   }
  13. }
  14. int main(int ac, char * av[]){
  15.   struct addrinfo souhait, * info;
  16.   int guichet, data, t;
  17.   char buffer[1024], * reponse;
  18.   if (ac != 2){
  19.     fprintf(stderr, "usage: %s port\n", av[0]);
  20.     exit(1);
  21.   }
  22.   memset(&souhait, 0, sizeof souhait);
  23.   souhait.ai_family = AF_UNSPEC;
  24.   souhait.ai_socktype = SOCK_STREAM;
  25.   souhait.ai_flags = AI_PASSIVE;
  26.   if ((t = getaddrinfo(0, av[1], &souhait, &info)) != 0){
  27.     fprintf(stderr, "erreur %s sur le port %s\n", gai_strerror(t), av[1]);
  28.     exit(1);
  29.   }
  30.   // fabriquer la socket et y obtenir une connexion sur une autre socket
  31.   try(guichet = socket(info->ai_family, info->ai_socktype, info->ai_protocol), "socket" );
  32.   try(bind(guichet, info->ai_addr, info->ai_addrlen), "bind" );
  33.   try(listen(guichet, 1), "listen" );
  34.   try(data = accept(guichet, 0, 0), "accept" );
  35.   try(close(guichet), "close" );
  36.   // lire, traiter, répondre
  37.   while((t = read(data, buffer, sizeof buffer)) > 0){
  38.     global_sock = data;
  39.     reponse = traiter(buffer, t);
  40.     try(write(data, reponse, strlen(reponse)), "write" );
  41.   }
  42.   try(t, "read" );
  43.   exit(0);
  44. }


 
Merci d'avance pour l'aide !

Reply

Marsh Posté le 23-03-2016 à 14:00:11   

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed