The socket for the Python script

Source: Internet
Author: User

############################################# #socket单线程

Server side:

#!/usr/bin/env python

Import Socket,time,os

Host= "

port=18000

S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)

S.bind ((Host,port))

S.listen (1)

While 1:

Conn,addr=s.accept ()

print ' connected by ', addr

While 1:

DATA=CONN.RECV (8192)

Cmd=os.popen (data)

Cmd_smg= ' \033[32;1mfeedback of the cmd\033[0m\n ' +cmd.read ()

If not data:break

#conn. Sendall (Data.upper ())

Conn.sendall (CMD_SMG)

print ' connected by ', addr

#print ' receievd cmd: '%data.upper ()

Print CMD_SMG

Conn.close ()

---------------------------------------------------

Client:

#!/usr/bin/env python

#_ *_ Coding:utf-8 _*_

Import Socket,time,random,tab

Host= ' 192.168.1.10 '

port=18000

S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)

S.connect ((Host,port))

While 1:

#smg =random.randint (1,100)

Cmd=raw_input (' input cmd: ')

#s. Sendall (' Hello my name is xiaohong:%s '%smg)

S.sendall (CMD)

DATA=S.RECV (8192)

Print "Received from server:", data

Time.sleep (1.2)

S.close ()

################################################## #socket Server Multithreading

Server Side

#!/usr/bin/env python

Import Socketserver

Class Mytcphandle (Socketserver.baserequesthandler):

def handle (self):

While 1:

SELF.DATA=SELF.REQUEST.RECV (8192)

If not self.data:

print ' Connected break '

Break

print ' Connected from: ', self.client_address

Print Self.data

Self.request.sendall (Self.data.upper ())

Host,port= ', 9999

Server=socketserver.threadingtcpserver ((host,port), Mytcphandle)

Server.serve_forever ()

-----------------------------------------------------

Client:

#!/usr/bin/env python

#_ *_ Coding:utf-8 _*_

Import Socket,time,random,tab

Host= ' 192.168.1.10 '

port=9999

S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)

S.connect ((Host,port))

While 1:

#smg =random.randint (1,100)

Cmd=raw_input (' input cmd: ')

Cmd=cmd.strip ()

If Len (cmd) ==0:continue

#s. Sendall (' Hello my name is xiaohong:%s '%smg)

S.sendall (CMD)

DATA=S.RECV (8192)

Print "Received from server:", data

#time. Sleep (1.2)

S.close ()

########################################################################






This article is from the "Galaxy | Computer network" blog, please be sure to keep this source http://qqran.blog.51cto.com/10014850/1961981

The socket for the Python script

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.