2. Python Socket Programming

Source: Internet
Author: User

1. Get Baidu return information

Import sockethost = "www.baidu.com" port = 80ip = Socket.gethostname (host) #获取访问百度的时候解析出来的IP地址. (VIP) Print ("Connect server IP = {0}". Format (IP)) s = socket.socket (socket.af_inet, socket. Sock_stream) s.connect ((IP, port)) #s. Sendall ("") #python2在sendall () must be in the form of a string (str), not in this form. S.sendall ("get/http/1.1 \r\nhost:www.baidu.com\r\n\r\n". Encode ("Utf-8")) #python3在sendall () must be in bytes (beat) Form # In the development of the HTTP level of the protocol, Baidu has its own rules, if sent past the string does not comply with the rules, Baidu will not return information, to comply with the other party's rules. data = S.RECV (2048) #接收数据print (data)

2.socket SSH Connection Terminal

Client:

import socketimport timeif __name__ ==  "__main__":     host =   "192.168.0.103"     port = 12345    s =  Socket.socket (Socket.af_inet, socket. Sock_stream)     s.connect ((host, port))     stat = 1     while 1:        cmd = raw_input ("{0} >>> ". Format (host))         if cmd.lower ( )  ==  "Exit":             exit ()          if not cmd:             continue        s.sendall (CMD)          DATA = S.RECV (2048)          if len (data)  > 0:             stat = 1             print  ("{0}". Format (data))         else:             stat += 1             time.sleep (1)              if stat == 5:                 break

server:

import socketimport commandsimport timehost =  "192.168.0.103" port = 12345s  = socket.socket (Socket.af_inet, socket. SOCK_STREAM) print  ("Server host = {0} port = {1}". Format (Host, port)) S.bind ((Host, port)) S.listen (6) conn, addrclient = s.accept () print  ("Connect from  {0} ". Format (addrclient)) stat = 1while 1:    cmd = conn.recv (2048)     if len (CMD)  > 0:         stat = 1        status, output =  Commands.getstatusoutput (cmd)         conn.sendall (output)      else:        stat += 1         time.sleep (1)        &Nbsp;if stat == 5:            break 

This script also lacks a lot of functions, there are many bugs, but also to be later to improve and repair, to be continued ing ...


3. Modules dedicated to SSH

Modules specifically for SSH connections in the Python module: Paramiko

Web page can be connected to the server, based on the SSH protocol, do not need to write the server

is an SSH version of the client

Installing Paramiko

# pip Install Paramiko


To create a connection script:

Vim para.py #注意名字不能用paramiko. PY, otherwise there will be a problem during the execution of the script.

Import timeimport paramikoclient = paramiko. Sshclient () Client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())       #用来设置know-hostclient.connect (hostname= "192.168.0.103",  port =22, username= "Root",  password= "1q2w3e") stat = 1while 1:    cmd  = raw_input ("{0}>> ". Format (' 192.168.0.103 '))     if cmd:         stat = 1         Stdin, stdout, stderr = client.exec_command (CMD)          for std in stdout.readlines ():             print  (STD)     else:         stat += 1        time.sleep (1)          if stat == 5:             Break

Execute to connect to the server.

2. Python Socket Programming

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.