Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:25:43+00:00 2026-06-12T02:25:43+00:00

I want to send a simple string from Python 3 to a PHP site

  • 0

I want to send a simple string from Python 3 to a PHP site that converts it into a .txt file. My entire code looks like this:

Python:

import urllib.parse
import urllib.request

str1 = "abcdefg"

url = "http://site.net/post.php"

aa = str1.encode('utf-8')
req = urllib.request.Request(url, aa)
req.add_header('Content-Type', 'this/that')
urllib.request.urlopen(req, data=aa)

PHP:

<?php

$handle = fopen("/dir/".name.".txt", "w");

$myContent = $_POST[aa];

fwrite($handle, $myContent);

fclose($handle);

?>

Python accesses the site, and a .txt file gets created, except the file is blank. I’ve tried changing $_POST to $_GET and $_REQUEST, as well as placing single and double quotes around 'aa' in various places. I suspect that Python and PHP aren’t communicating about the name of the string/data that I want it to interpret.

EDIT: This PHP code already handles POST data from another website. The issue only arises with Python compatibility

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:25:44+00:00Added an answer on June 12, 2026 at 2:25 am

    The data you’re sending is invalid. Following the documentation:

    data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.parse.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format. It should be encoded to bytes before being used as the data parameter.

    The documentation also contains a simple example you just need to follow:

    import urllib.request
    import urllib.parse
    
    data = urllib.parse.urlencode({'spam': 1, 'eggs': 2, 'bacon': 0})
    data = data.encode('utf-8')
    request = urllib.request.Request("http://requestb.in/xrbl82xr")
    
    # adding charset parameter to the Content-Type header.
    request.add_header("Content-Type","application/x-www-form-urlencoded;charset=utf-8")
    
    f = urllib.request.urlopen(request, data)
    print(f.read().decode('utf-8'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write code to send a simple mail from asp.net page.
Basically i just want to get cookies from server. Here's simple code, but i
I often want to send sample apps to friends & colleages that work with
Hello Sir i want send list of data to php server i use following
Want to send the text from my current vb application to the Active Window
I want to send a opened file descriptor between two different programs. So I
I want to send a few variables and a string with the POST method
I'm trying make my first python app. I want make simple email sender form.
I want to send my C# string to a C++ DLL function. I have
I want to store the data received from serial port in a string variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.