• python socket编程


    一、什么是socket?

      Python 官方关于 Socket 的函数请看 http://docs.python.org/library/socket.html

      socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求。

      socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用打开、读写、关闭模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

      file模块是针对某个指定文件进行打开、读写、关闭

      socket模块是针对 服务器端 和 客户端Socket 进行打开、读写、关闭

    二、举个实例

     1 #!/usr/bin/env python
     2 # _*_ coding: UTF-8 _*_
     3 # Author:taoke
     4 import socket
     5 import sys
     6 try:
     7     #创建socket
     8     s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
     9 except socket.error:
    10     #创建失败,产生socket.error异常
    11     print("socket creat error:"+str(socket.error))
    12     sys.exit()
    13 
    14 print("creat socket")
    15 host = 'www.oschina.net'
    16 
    17 try:
    18     #获取主机IP
    19     remote_ip = socket.gethostbyname(host)
    20 except socket.gaierror:
    21     print("hostname could not be resolved,exiting")
    22     sys.exit()
    23 print("IP address of "+host+" is "+remote_ip)
    24 port = 80
    25 #连接
    26 s.connect((remote_ip,port))
    27 print('Socket Connected to ' + host + ' on ip ' + remote_ip)
    28 #Send some data to remote server
    29 message = "GET / HTTP/1.1
    Host: oschina.net
    
    "
    30 
    31 try :
    32     #Set the whole string
    33     s.sendall(message.encode())
    34 except socket.error:
    35     #Send failed
    36     print('Send failed')
    37     sys.exit()
    38 
    39 print('Message send successfully')
    40 
    41 #Now receive data
    42 reply = s.recv(4096)
    43 
    44 print(reply.decode())
    45 #关闭socket
    46 s.close()

    运行结果如下:

    creat socket
    IP address of www.oschina.net is 139.199.91.153
    Socket Connected to www.oschina.net on ip 139.199.91.153
    Message send successfully
    HTTP/1.1 301 Moved Permanently
    X-Proxy: dayu-proxy
    Date: Wed, 20 Sep 2017 13:28:07 GMT
    Content-Type: text/html
    Content-Length: 278
    X-DAYU-UUID: D7PRC9C6CFCF6932495F893F61E66ABDB60C
    Connection: keep-alive
    Set-Cookie: __DAYU_PP=733na7yMm7VYJ7VbeQz3ffffffffef192cb9ef6a; Expires=Wed, 09 Jun 2021 23:59:59 GMT; Path=/
    Location: http://www.oschina.net/
    
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html>
    <head><title>301 Moved Permanently</title></head>
    <body bgcolor="white">
    <h1>301 Moved Permanently</h1>
    <p>The requested resource has been assigned a new permanent URI.</p>
    <hr/>Powered by Tengine</body>
    </html>

    TCP客户端:

    1 创建套接字,连接远端地址

            # socket.socket(socket.AF_INET,socket.SOCK_STREAM) , s.connect()

    2 连接后发送数据和接收数据        

       # s.sendall(), s.recv()

    3 传输完毕后,关闭套接字        

       #s.close()

  • 相关阅读:
    poj 3616 Milking Time
    poj 3176 Cow Bowling
    poj 2229 Sumsets
    poj 2385 Apple Catching
    poj 3280 Cheapest Palindrome
    hdu 1530 Maximum Clique
    hdu 1102 Constructing Roads
    codeforces 592B The Monster and the Squirrel
    CDOJ 1221 Ancient Go
    hdu 1151 Air Raid(二分图最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/taoke2016/p/7565260.html
Copyright © 2020-2023  润新知