简体   繁体   English

连接python套接字时从服务器清空响应

[英]Empty response from server when connecting with python socket

I am trying to connect to URL https://www.ssehl.co.uk/HALO/publicLogon.do in Python. 我正在尝试使用Python连接到URL https://www.ssehl.co.uk/HALO/publicLogon.do

The simple solution using requests fails: 使用requests的简单解决方案失败:

import requests
r = requests.get('https://www.ssehl.co.uk/HALO/publicLogon.do')
print r.text

with error 有错误

File "c:\Python27\lib\site-packages\requests\adapters.py", line 327, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='www.ssehl.co.uk', port=443): Max retries exceeded with url: /HALO/publicLogon.do (Caused by <class 'httplib.BadStatusLine'>: '')

so I tried to get the raw response from the server using library socket : 所以我试图使用库socket从服务器获取原始响应:

import socket   #for sockets
import sys  #for exit

#create an INET, STREAMing socket
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.ssehl.co.uk';
port = 443;

try:
    remote_ip = socket.gethostbyname(host)

except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()

#Connect to remote server
s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip

#Send some data to remote server
message = "GET /HALO/publicLogon.do HTTP/1.1\r\n\r\n"

try :
   #Set the whole string
   s.sendall(message)
except socket.error:
   #Send failed
   print 'Send failed'
   sys.exit()

print 'Message send successfully'

#Now receive data
reply = s.recv(4096)

print reply

will output: 将输出:

Socket Created
Socket Connected to www.ssehl.co.uk on ip 161.12.7.194
Message send successfully
Reply: 

after reply there is some garbage which I can't paste, however this is a sublime console screenshot: 回复后有一些我无法粘贴的垃圾,但这是一个崇高的控制台截图:

Screenshot 截图

Is there any way to get a 200 response from the server, just like a browser? 有没有办法从服务器获得200响应,就像浏览器一样?

For some reason when you use either Python's built in stuff (urllib2, requests, httplib) or even command line stuff (curl, wget) over https the server spazzes out and gives an erroneous response. 出于某种原因,当您使用Python内置的东西(urllib2,请求,httplib)甚至命令行内容(curl,wget)而不是https时,服务器会出现并给出错误的响应。

However when you request the page over regular http, it works fine, for example: 但是,当您通过常规http请求页面时,它可以正常工作,例如:

import urllib2
print urllib2.urlopen('http://www.ssehl.co.uk/HALO/publicLogon.do').getcode()

prints out 打印出来

>> 200

My guess is that their servers are configured wrong and your browser somehow deals with it silently. 我的猜测是他们的服务器配置错误,你的浏览器以某种方式静默地处理它。

It worked for me when I used port 80 . 当我使用端口80时,它对我有用。 Sooo: port = 80; Sooo: port = 80;

There must be some error when using HTTPS servers thought Python... 使用HTTPS服务器认为Python时必定会出现一些错误...
Also, you are sending wrong request. 此外,您发送错误的请求。 You are not sending the hostname . 您没有发送hostname

Fixed request: message = "GET /HALO/publicLogon.do HTTP/1.1\\r\\nHostname: %s\\r\\n\\r\\n"%host 修复了请求: message = "GET /HALO/publicLogon.do HTTP/1.1\\r\\nHostname: %s\\r\\n\\r\\n"%host


So here is working code. 所以是工作代码。


I think the problem exists, because port 443 is encrypted . 我认为问题存在,因为端口443加密的 And Python doesn't support encryption (probably). Python不支持加密(可能)。

You should use ssl.wrap_socket if you want support https. 如果需要支持https,则应使用ssl.wrap_socket

See http://docs.python.org/2/library/ssl.html for details. 有关详细信息,请参见http://docs.python.org/2/library/ssl.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM