3

I am trying to connect to URL https://www.ssehl.co.uk/HALO/publicLogon.do in Python.

The simple solution using requests fails:

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:

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?

1
  • Hmm, the code using requests works for me. The requests.get line returns <Response [200]>, while the r.text part returns the webpage correctly. Probably temporary error on the server yesterday? Commented Oct 17, 2013 at 10:04

3 Answers 3

1

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.

However when you request the page over regular http, it works fine, for example:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

It worked for me when I used port 80. Sooo: port = 80;

There must be some error when using HTTPS servers thought Python...
Also, you are sending wrong request. You are not sending the hostname.

Fixed request: 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. And Python doesn't support encryption (probably).

Comments

0

You should use ssl.wrap_socket if you want support https.

See http://docs.python.org/2/library/ssl.html for details.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.