Python Socket Article __python

Source: Internet
Author: User

1. Installation:


2. Documentation:

Https://docs.python.org/3/library/socket.html


3.

client.py

"" "
import socket  
HOST =" 127.0.0.1 "  
PORT = 3333  
s = socket.socket (socket.af_inet, socket. SOCK_STREAM)  
s.connect (HOST, PORT) while  
1:  
    cmd = raw_input (' Your command: '). Strip ()  
    S.sendall ( CMD) Result  
    = S.RECV (1024)  
    print result  
s.close () ""















import socket
Import sys

Host = ' 127.0.0.1 '
port = 8888


s = Socket.socket (socket.af_inet,socket.  SOCK_STREAM)
print ' Socket Create '

remote_ip = socket.gethostbyname (host)
print ' IP address of ' + Host + ' Is ' + remote_ip

s.connect ((remote_ip,port))
print ' Socket Connected to ' + Host + ' on IP ' + remote_ip

m essage = ' Hello '
s.sendall (message)
print ' message send successfully '

reply = S.recv (4096)
print Reply

s.close ()



server.py

"" "Import socketserver, OS class Mytcphandler (Socketserver.baserequesthandler): def handle (self): while True:self.data = SELF.REQUEST.RECV (1024). Strip () print "{}wrote:". Format (self.client_address  
                [0] If not self.data:print ' clilent%s is dead! '% self.client_address[0]  
            Break Cmd_result = Os.popen (self.data). read ();  
                If Len (Cmd_result.strip ())!= 0:self.request.sendall (cmd_result) Else: Self.request.sendall (' not found the command: ' + self.data ') if __name__ = = "__main__": HOST, PORT = "127.0.0.1", 33





Server = Socketserver.threadingtcpserver (HOST, PORT, Mytcphandler) server.serve_forever () "" " Import socket import SYS import thread HOST = ' PORT = 8888 s = Socket.socket (socket.af_inet,socket. Sock_stream) A = Socket.getdefaulttimeout () print ' socket created ' try:s.bind ((Host,port)) 
Except Socket.error, Msg:print (str (msg[0) + ' +str (msg[1])) sys.exit () print ' socket bind complete ' s.listen (2) print ' Socket now listening ' Def clientthread (conn): Conn.send (' Welcome to the server. Type somthing and hit enter\n ') print (a) while True: #try: Data = CONN.RECV (9999) Reply = ' OK ... ' + data #except socket.error, msg: #print (str (msg[0]) + ' +str (msg[1)) #sys. Exit ( If not data:break Conn.sendall (reply) conn.close () while 1:conn,addr = S.accept ( print ' Connected with ' + addr[0] + ': ' + str (addr[1]) Thread.start_new_thread (Clientthread, (conn,)) s.cl
 OSE ()


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.