Python Simple socket

Source: Internet
Author: User

  1. Clinet End



  2. #! /usr/bin/env python# -*-coding: utf-8-*-#这段代码是clinet端import  socket     #for  socketsimport sys   #for  exittry:     #create  an  AF_INET, STREAM socket  (TCP)     s = socket.socket (socket.af _inet, socket. Sock_stream) except socket.error, msg:    print  ' Failed to create  socket. Error code:  '  + str (msg[0])  +  '  , Error  message :  '  + msg[1]    sys.exit ();p rint  ' Socket Created ' host =  ' 192.168.1.20 ' port = 8888try:    remote_ip =  Socket.gethostbyname ( host ) except socket.gaierror:     #could  not  resolve    print  ' hostname could not be resolved.  Exiting '      sys.exit () print  ' ip address of  '  + host +  '  is  '  + remote_ip#connect to remote servers.connect ((remote_ip , port)) print   ' socket connected to  '  + host +  '  on ip  '  +  Remote_ip
  3. Server-side

  4. #! /usr/bin/env python# -*- coding: utf-8 -*-import socketimport  sysfrom thread import *host =  '    # Symbolic name  Meaning all available interfacesport = 8888 # arbitrary non-privileged  ports = socket.socket (Socket.af_inet, socket. Sock_stream) print  ' socket created ' #Bind  socket to local host and  Porttry:    s.bind ((host, port)) except socket.error , msg:     print  ' bind failed. error code :  '  + str (msg[0])  +   '  Message  '  + msg[1]    sys.exit () print  ' Socket bind  complete ' #Start  listening on sockets.listen (Ten) print  ' socket now listening ' #Function  FOR HANDLING CONNECTIONS. THIS WILL&NBsp;be used to create threadsdef clientthread (conn):     #Sending  message to connected client    conn.send (' Welcome to the  server. type something and hit enter\n ')   #send  only takes  string     #infinite  loop so that function do not  terminate and thread do not end.    while true:          #Receiving  from client         DATA = CONN.RECV (1024x768)         reply =  ' OK ... '  + data        if not data:             break         Conn.sendall (Reply)      #came  out of loop    conn.close () #now  keep talking  with the clientwhile 1:     #wait  to accept a  Connection - blocking call    conn, addr = s.accept ()      print  ' connected with  '  + addr[0] +  ': '  + str ( ADDR[1])      #start  new thread takes 1st argument as a  function name to be run, second is the tuple of  Arguments to the function.    start_new_thread (CLIENTTHREAD , (conn,)) S.close ()

Python Simple socket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.