RISOLTO Traduzione da C in Linguaggio Python

Pubblicità

Giovani2015

Nuovo Utente
Messaggi
26
Reazioni
0
Punteggio
23
Ciao a tutti,
Ho implementato in C una funzione client.c per provare una connessionedel socket ad un indirizzo IP locale e una porta TCP locale tramite un'altra funzione server.c.
Ora vorrei tradurre il mio programma in Python pero incontro un po' di difficoltà.
All'esecuzione, la funzione client.c avrà 2 argomenti da mettere nella riga di comando, l'hostname e il port.

Esempio:
./client.c 127.0.0.1 1234

Ecco la mia funzione:

client.c

Codice:
void try(int result, char * name){
  if (result < 0){
    perror(name);
    exit(1);
  }
}

int main(int ac, char * av[]){
  struct addrinfo souhait, * info;
  int sock, t;
  char buffer[1024];

  if (ac != 3){
    fprintf(stderr, "usage: %s machine port\n", av[0]);
    exit(1);
  }

  memset(&souhait, 0, sizeof souhait);
  souhait.ai_family = AF_UNSPEC;
  souhait.ai_socktype = SOCK_STREAM;
  if ((t = getaddrinfo(av[1], av[2], &souhait, &info)) != 0){
    fprintf(stderr, "error %s in %s:%s\n", gai_strerror(t), av[1], av[2]);
    exit(1);
  }

  try(sock = socket(info->ai_family, info->ai_socktype, info->ai_protocol), "socket");
  try(connect(sock, info->ai_addr, info->ai_addrlen), "connect");

  while((t = read(0, buffer, sizeof buffer)) > 0){
    write(sock, buffer, t);
    if ((t = read(sock, buffer, sizeof buffer)) <= 0)
      break;
    write(1, buffer, t);
  }
  try(t, "read");
  exit(t < 0);
}

Se qualcuno potrebbe aiutarmi.

Grazie in anticipo !
 
Hai provato a guardare la documentazione ufficiale? Mi sembra abbastanza esaustiva... https://docs.python.org/3/library/socket.html

Ciao 1nd33d,
Grazie per la tua risposta. L'ho appena guardata pero il mio problema sopratutto è di passare gli argomenti HOST e PORT sulla riga di comando all'esecuzione invece di essere nel codice.

Ad esempio fare ./client 127.0.0.1 1234 invece di ./client sulla base del codice sotto:

Codice:
import socket
import sys

HOST = "127.0.0.1"    # Il nodo remoto
PORT =  1234          # porta usata dal server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
    s = socket.socket(af, socktype, proto)
    except socket.error, msg:
    s = None
    continue
    try:
    s.connect(sa)
    except socket.error, msg:
    s.close()
    s = None
    continue
    break
if s is None:
    print 'could not open socket'
    sys.exit(1)

while 1:
   msg = raw_input('>> ')
   s.send(msg)

http://www.tomshw.it/forum/members/1nd33d-80939.html
 
Ultima modifica da un moderatore:
La getaddrinfo non ti serve realmente. Puoi semplicemente fare:
Codice:
import sys, socket

if len(sys.argv) != 3:
   # gestisci errore


host = sys.argv[1]
port = sys.argv[2]

s = socket.socket()
s.connect( (host, port) ) # tuple host-port

msg = input()
while msg:
   s.send( bytes(msg, 'utf-8') )
   msg = input()
È Python 3, ma il discorso è lo stesso per il 2.
 
Pubblicità
Pubblicità
Indietro
Top