Python的 Socket编程接收数据

import socket
import sys
try:
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
except socket.error:
    print('Failed to create socket')
    sys.exit()
print("Socket Created")

host='www.oschina.net'
port=80

try:
    remote_ip=socket.gethostbyname(host)
except socket.gaierror:
    print('Hostname could not be resolved .Exiting')
    sys.exit()

print("Ip address of ",host,'is',remote_ip)

s.connect((remote_ip,port))

print('Socket Connected to ',host,"on ip",remote_ip)

#Send some data to remote server
try:
    #Set the whole string
    s.sendall(b'GET / HTTP/1.1\r\n\r\n')
except socket.error:
    print('Send failed')
    sys.exit()

print("Message send successfully")

#Now receive data
reply=s.recv(1024)
s.close()
print(repr(reply))

你可能感兴趣的:(socket编程)