maketrans(x, [y, [z]])

This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab and outtab must have the same length.

라고 써져있는데.. 음.. 테이블맵에 맞춰서 변환해준다는 것같다. 테이블맵을 짠후에는 translate()함수를 사용하여, 문자열을 바꿔주는 함수.

 

Maketrans(table [,deletechars])

- translate()함수에 쓰일 번역용 맵을 반환

- 인자가 하나일 경우 사전형식 입력

- 인자가 둘인경우 길이가 같은 문자열을 입력받아 x의 문자가 y로 변환

- 인자가 셋인경우 x가 y로 변환되고 z문자가 None으로 대체(삭제)

 

Help on built-in function translate:

translate(...)
    S.translate(table [,deletechars]) -> string
   
    Return a copy of the string S, where all characters occurring
    in the optional argument deletechars are removed, and the
    remaining characters have been mapped through the given
    translation table, which must be a string of length 256 or None.
    If the table argument is None, no translation is applied and
    the operation simply removes the characters in deletechars.

 

ex)

>>>a= "abcd"

>>>b=string.maketrans("abcd","efgt")

>>>c = a.translate(b)

>>>c

'efgt'

>>>

 

 

대부분의 사이트와 블로그에선 str.maketrans로 나오지만 내 우분투(11.10)파이썬(2.7.2)에서는 명령어가 먹히지 않았다. 대신 string.maketrans 를 사용해야함.

'Programming > Python' 카테고리의 다른 글

Python Challenge 4  (0) 2012.05.23
Python challenge 3  (1) 2012.05.23
Python challenge 2  (0) 2012.05.23
Python challenge 1  (0) 2012.05.15
Python challenge 0  (0) 2012.05.15

#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdlib.h>

 

int main(int argc,char *argv[]){

        char buf[100],ip;
        int sock_serv,sock_clin,i;
        struct sockaddr_in serv_adr,clin_adr,test;
        int str_len;
        FILE *fp;
        socklen_t adr_sz;
        pid_t pid;

 

        if(argc != 2)
                printf("Please Input <Port>");

 

        sock_serv=socket(PF_INET,SOCK_STREAM,0);
        memset(&serv_adr,0,sizeof(serv_adr));
        serv_adr.sin_family=AF_INET;
        serv_adr.sin_port=htons(atoi(argv[1]));
        serv_adr.sin_addr.s_addr=htonl(INADDR_ANY);

 

        if(bind(sock_serv,(struct sockaddr*)&serv_adr,sizeof(serv_adr))==-1)
                printf("bind error()");
        if(listen(sock_serv,5)==-1)
                printf("listen error()");

 

        while(1){
                adr_sz=sizeof(clin_adr);
                sock_clin=accept(sock_serv,(struct sockaddr*)&clin_adr,&adr_sz);
                memset(&test,0,sizeof(test));                
                getpeername(sock_clin,(struct sockaddr *)&test,&adr_sz);

//클라이언트의 주소정보를 getpeername함수를 이용하여 test구조체에 저장한다.


                if(sock_clin==-1){
   }
                else{
                        printf("Accept at : %s!!\n",inet_ntoa(test.sin_addr)); //클라이언트의 ip출력.
                        pid=fork();
                        if(pid==0){            //자식프로세스 영역.
                                close(sock_serv);
                                while(str_len=read(sock_clin,buf,100)!=0){
                                i=strlen(buf);
                                write(sock_clin,buf,i);
                                fp=fopen("test.txt","a");        //test.txt파일을 열고.
                                fprintf(fp,"%s\t%s",inet_ntoa(test.sin_addr),buf);  //클라이언트의 ip와 buf저장.
                                fclose(fp);
                                }
                                close(sock_clin);
                                printf("Clinet disconnected\n");
                        }
                        else
                                close(sock_clin);
                }
        }
        close(sock_serv);
        return 0;
}

 

 

test.txt를 살펴보니.

127.0.0.1과 203.x.x.x에서 접속을 했고, 입력했던 문자열이 저장되어 있다.

 

'Programming > Socket' 카테고리의 다른 글

Select 함수  (0) 2012.05.16
I/O 멀티플렉싱  (0) 2012.05.15
클라이언트 IP 가져오기  (0) 2012.05.14
멀티프로세스 소스  (0) 2012.05.10
멀티프로세스 서버  (0) 2012.05.08

서버소켓에서 나에게 접속한 클라이언트 IP를 가져오는 함수가 존재한다.

로그파일을 만들 때 사용하는 건가?

 

int Getpeername( sock, struct sockaddr* name, int *size )

sock : IP를 알고자 하는 소켓의 파일 디스크립터. 
struct sockaddr* name : 소켓 정보를 저장할 sockaddr_in구조체

int *size : 구조체 사이즈.

 

ex)

struct sockaddr_in test_adr;

 

adr_sz=sizeof(clin_adr);

getpeername(sock,(struct sockaddr*)&test_adr,&adr_sz);

 

위와 같이 선언한다면, test_adr구조체 변수안에 접속한 클라이언트의 정보가 기록된다.

하지만 위와 같이 선언하고 출력해보면.

 

우리가 알수 없는 정수형으로 값이 저장되어 있다.

 

sockaddr_in 구조체의 주소멤버의 데이터 타입은 unsigned long이다.

따라서 네트워크바이트 순서인 32비트 값을 변환시켜주어야 한다.

 

이러한 경우에 사용하는 함수가 inet_ntoa()함수이다.

 

char *inet_ntoa(struct in_addr addr)

 

성공시 변환된 해당 문자열의 포인터를 반환하고, 실패할 경우에는 -1을 반환한다.

 

 


'Programming > Socket' 카테고리의 다른 글

I/O 멀티플렉싱  (0) 2012.05.15
로그파일을 남기는 서버  (0) 2012.05.14
멀티프로세스 소스  (0) 2012.05.10
멀티프로세스 서버  (0) 2012.05.08
소켓의 옵션  (0) 2012.05.07

+ Recent posts